CodeIgniter User Guide Version 2.1.0


HTML 테이블

Table 클래스는 데이터베이스나 배열로 부터 HTML 테이블을 자동으로 생성할 수 있도록 해줍니다.

클래스 초기화 Initializing the Class

다른 클래스들 처럼 , Table 클래스도 컨트롤러에서 $this->load->library 함수로 초기화 합니다:

$this->load->library('table');

일단 로드되면 , $this->table 을 통해서 테이블 객체를 이용합니다.

예제 Examples

이번 예제는 다차원 배열로부터 어떻게 테이블을 만드는지를 보여줍니다. 주의할점은 첫번째 배열인덱스가 테이블의 제목(heading)이 된다는 점입니다. set_heading() 함수를 이용하면 여러분이 직접 제목을 작성하실수도 있습니다.그 방법은 저 아래서 설명합니다.

$this->load->library('table');

$data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')
             );

echo $this->table->generate($data);

이번 예제는 데이터베이스 쿼리결과로 테이블을 만드는것을 보여줍니다.테이블 클래스는 테이블 이름들로부터 제목(heading)을 자동으로 설정합니다. set_heading() 함수를 이용하면 여러분이 직접 제목을 작성하실수도 있습니다.그 방법은 저 아래서 설명합니다.

$this->load->library('table');

$query = $this->db->query("SELECT * FROM my_table");

echo $this->table->generate($query);

이번 예제는 각각의 파라미터를 이용해서 테이블을 만드는법을 보여줍니다:

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');

$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');

echo $this->table->generate();

이번예제는 위와 유사하나 개별파라미터가 아닌 배열을 통해 테이블을 만드는법을 보여줍니다:

$this->load->library('table');

$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'));

echo $this->table->generate();

테이블 모양 바꾸기 Changing the Look of Your Table

여러분이 원하는 레이아웃에 맞추어 테이블 템플릿을 설정할 수 있습니다. 아래는 기본적인 테이블 템플릿 설정 예제입니다:

$tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="4" cellspacing="0">',

                    '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);

참고:  템플릿블록안에는 두 세트의 "row" 블록이 있다는것을 알 수 있습니다. 이렇게하면 한줄(row)마다 반복되는 다른 색 혹은 디자인을 적용할수 있습니다.(예를들어 첫줄 빨간색, 둘째줄 파란색 ,셋째줄 빨간색 , 네째줄 파란색 .......... 이런식 )

완벽한 구조의 템플릿을 만들필요는 없습니다. 테이블의 일부분만 바꾸고싶다면, 그부분만 템플릿을 만들면 됩니다. 이번예제는 테이블을 여는 태그만 바뀌게 됩니다:

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

$this->table->set_template($tmpl);

함수 레퍼런스 Function Reference

$this->table->generate()

생성된 테이블을 포함한 문자열을 리턴합니다. 배열이나 데이터베이스 결과 객체를 파라미터로 받습니다.

$this->table->set_caption()

테이블의 캡션을 설정합니다.

$this->table->set_caption('Colors');

$this->table->set_heading()

테이블의제목(heading)을 설정합니다. 배열이나 , 각각의 제목을 파라미터로 넘겨줄수 있습니다:

$this->table->set_heading('Name', 'Color', 'Size'); $this->table->set_heading(array('Name', 'Color', 'Size'));

$this->table->add_row()

한줄(row)을 테이블에 추가합니다.배열이나 혹은 각 값을 넘겨줄수 있습니다:

$this->table->add_row('Blue', 'Red', 'Green'); $this->table->add_row(array('Blue', 'Red', 'Green'));

각 셀의 태그에 속성을 부여하려면 연관배열을 사용하세요.데이터를 채우려면'data' 키를 사용하시고, 다른 키들은 모두 key='값' 의 형식으로 추가됩니다. :

$cell = array('data' => 'Blue', 'class' => 'highlight', 'colspan' => 2);
$this->table->add_row($cell, 'Red', 'Green');

// generates
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>

$this->table->make_columns()

이 함수는 1차원 배열을 받아서 원하는 칼럼수에 맞추어 다차원배열을 생성합니다. 이 함수를 통해서 값은 1차원배열인데, 표현하고자하는 칼럼의 개수가 그것보다 작은경우를 효과적으로 처리할수 있습니다.아래 예제를 보시면 이해가 쉬우실거예요:

$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');

$new_list = $this->table->make_columns($list, 3);

$this->table->generate($new_list);

// Generates a table with this prototype

<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr><tr>
<td>four</td><td>five</td><td>six</td>
</tr><tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr><tr>
<td>ten</td><td>eleven</td><td>twelve</td></tr>
</table>

$this->table->set_template()

여러분이 만든 템플릿을 적용할 수 있도록 해 줍니다. 전체템플릿이나, 부분템플릿을 적용하실 수 있습니다..

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

$this->table->set_template($tmpl);

$this->table->set_empty()

테이블셀의 값이 없을경우 기본값을 설정할수 있습니다.예를들어 아래와같이 한칸의 공백을 표시하는 예약어를 기본값으로 설정할수 있습니다:

$this->table->set_empty("&nbsp;");

$this->table->clear()

테이블을 의 제목과 데이터를 모두 비웁니다. 포함하고 있는 데이터가 다른 여러테이블을 보여줘야할경우 이전 테이블의데이터를 비우는데 사용합니다. 예:

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');

echo $this->table->generate();

$this->table->clear();

$this->table->set_heading('Name', 'Day', 'Delivery');
$this->table->add_row('Fred', 'Wednesday', 'Express');
$this->table->add_row('Mary', 'Monday', 'Air');
$this->table->add_row('John', 'Saturday', 'Overnight');

echo $this->table->generate();

$this->table->function

모든 셀 데이터에 PHP내장함수나 사용가능한 함수배열 객체 (function array object)를 적용합니다

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', '<strong>Blue</strong>', 'Small');

$this->table->function = 'htmlspecialchars';
echo $this->table->generate();

위의 예제에서는 모든 셀 데이터에 PHP의 htmlspecialchars() 함수가 적용되고, 결과는 다음과 같습니다 :

<td>Fred</td><td>&lt;strong&gt;Blue&lt;/strong&gt;</td><td>Small</td>