HTML 테이블(table) Sample

<?php
/**
 * Table Class
 *
 * Created on 2011. 11. 16.
 * @author 불의회상 <hoksi2k@hanmail.net>
 * @package library
 * @subpackage controllers
 * @version 1.0
 */
class Table_lib extends CI_Controller {
	function __construct() {
		parent::__construct();
	}
	
	function index() {
		$this->load->database();
		$this->load->library('table');
		
		// Default
		$tbl_data = array(
			array('Name', 'Color', 'Size'),
			array('Fred', 'Blue', 'Small'),
			array('Mary', 'Red', 'Large'),
			array('John', 'Green', 'Medium')
		);
		$data['default'] = $this->table->generate($tbl_data);
		
		// DataBase를 통한 생성
		$query = $this->db->query("SELECT * FROM my_table");
		$data['db_gen'] = $this->table->generate($query);

		// 파라미터를 이용하여 생성
		$this->table->set_heading('이름', '색상', '크기');
		
		$this->table->add_row('Fred', 'Blue', 'Small');
		$this->table->add_row('Mary', 'Red', 'Large');
		$this->table->add_row('John', 'Green', 'Medium');
		
		$data['param_gen'] = $this->table->generate();
		
		// 배열을 이용하여 생성
		$this->table->set_heading(array('Name', 'Color', 'Size'));
		
		$this->table->add_row(array('Fred', 'Blue', 'Small'));
		$this->table->add_row(array('Mary', 'Red', 'Large'));
		$this->table->add_row(array('John', 'Green', 'Medium'));
		
		$data['array_gen'] =  $this->table->generate();
		
		// 템플릿 변경
		$tmpl = array (
			'table_open'          => '<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">',
		
			'heading_row_start'   => '<tr>',
			'heading_row_end'     => '</tr>',
			'heading_cell_start'  => '<th>',
			'heading_cell_end'    => '</th>',
		
			'row_start'           => '<tr>',
			'row_end'             => '</tr>',
			'cell_start'          => '<td>',
			'cell_end'            => '</td>',
		
			'row_alt_start'       => '<tr>',
			'row_alt_end'         => '</tr>',
			'cell_alt_start'      => '<td>',
			'cell_alt_end'        => '</td>',
		
			'table_close'         => '</table>'
		);
		
		$this->table->set_template($tmpl);
		$data['tmpl_all'] = $this->table->generate($tbl_data);

		// 템플릿 일부 항목 변경
		$tmpl = array ( 'table_open'  => '<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">' );

		$this->table->set_template($tmpl);		
		$data['tmpl_sub'] = $this->table->generate($tbl_data);
		
		// $this->table->make_columns()
		// 1차원 배열 => 다차원배열 생성
		$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
		$new_list = $this->table->make_columns($list, 3);
		$data['new_list'] = $this->table->generate($new_list);

		// $this->table->function
		// 모든 셀 데이터에 PHP내장함수나 사용가능한 함수배열 객체 (function array object)를 적용
		$this->table->set_heading('Name', 'Color', 'Size');
		$this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');
		
		// 모든 셀 데이터에 PHP의 htmlspecialchars() 함수가 적용
		$this->table->function = 'htmlspecialchars';
		
		$data['table_function'] = $this->table->generate();
		
		$this->load->view('table_lib_sample', $data);
	}
}
<?php $this->load->view('inc/header')?>

<table class="tablesorter" border="0" cellpadding="0" cellspacing="1">
<thead>
<tr>
  <th style="text-align:center" width="15%">구분</th>
  <th width="35%">결과</th>
  <th style="text-align:center" width="15%">구분</th>
  <th width="35%">결과</th>
</tr>
</thead>
<tr>
	<td>Default</td>
	<td><?php echo $default?></td>
	<td>DB를 통한 생성</td>
	<td><?php echo $db_gen?></td>
</tr>
<tr>
	<td>파라미터를 이용하여 생성</td>
	<td><?php echo $param_gen?></td>
	<td>배열을 이용하여 생성</td>
	<td><?php echo $param_gen?></td>
</tr>
<tr>
	<td>템플릿 사용</td>
	<td><?php echo $tmpl_all?></td>
	<td>템플릿 일부 항목 변경</td>
	<td><?php echo $tmpl_sub?></td>
</tr>
<tr>
	<td>$this->table->make_columns()</td>
	<td><?php echo $new_list?></td>
	<td>$this->table->function</td>
	<td><?php echo $table_function?></td>
</tr>
</table>

<?php $this->load->view('inc/footer')?>

© Copyright by hoksi(Page rendered in 0.0190 seconds)