HTML 테이블 클래스

Table 클래스는 배열 또는 데이터베이스 결과 집합에서 HTML 테이블을 자동으로 생성할 수 있는 함수를 제공합니다.

Table 클래스 사용

클래스 초기화

CodeIgniter의 대부분의 클래스와 마찬가지로 Table 클래스는 컨트롤러에서 $this->load->library() 메소드를 사용하여 초기화합니다:

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

로드되면 다음을 사용하여 Table 라이브러리 객체에 접근할 수 있습니다:

$this->table

예시

다음은 다차원 배열에서 테이블을 만드는 방법을 보여주는 예시입니다. 첫 번째 배열 인덱스가 테이블 제목이 됩니다(또는 아래 함수 레퍼런스에 설명된 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);

다음은 데이터베이스 쿼리 결과에서 만든 테이블 예시입니다. Table 클래스는 테이블 이름을 기반으로 자동으로 제목을 생성합니다(또는 아래 클래스 레퍼런스에 설명된 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();

테이블 모양 변경

Table 클래스를 사용하면 레이아웃 디자인을 지정할 수 있는 테이블 템플릿을 설정할 수 있습니다. 다음은 템플릿 프로토타입입니다:

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

        'thead_open'            => '<thead>',
        'thead_close'           => '</thead>',

        'heading_row_start'     => '<tr>',
        'heading_row_end'       => '</tr>',
        'heading_cell_start'    => '<th>',
        'heading_cell_end'      => '</th>',

        'tbody_open'            => '<tbody>',
        'tbody_close'           => '</tbody>',

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

참고

템플릿에 두 세트의 “row” 블록이 있음을 알 수 있습니다. 이를 통해 행 데이터의 각 반복과 함께 교대로 바뀌는 행 색상 또는 디자인 요소를 만들 수 있습니다.

완전한 템플릿을 제출할 필요는 없습니다. 레이아웃의 일부만 변경해야 하는 경우 해당 요소만 제출하면 됩니다. 이 예시에서는 테이블 여는 태그만 변경됩니다:

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

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

설정 파일에서 이에 대한 기본값을 설정할 수도 있습니다.

클래스 레퍼런스

class CI_Table
$function = NULL

모든 셀 데이터에 적용할 기본 PHP 함수 또는 유효한 함수 배열 객체를 지정할 수 있습니다:

$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>
generate([$table_data = NULL])
매개변수:
  • $table_data (mixed) – 테이블 행을 채울 데이터

반환:

HTML 테이블

반환 형식:

string

생성된 테이블이 포함된 문자열을 반환합니다. 배열 또는 데이터베이스 결과 객체가 될 수 있는 선택적 매개변수를 허용합니다.

set_caption($caption)
매개변수:
  • $caption (string) – 테이블 캡션

반환:

CI_Table 인스턴스 (메소드 체이닝)

반환 형식:

CI_Table

테이블에 캡션을 추가할 수 있습니다:

$this->table->set_caption('Colors');
set_heading([$args = array()[, ...]])
매개변수:
  • $args (mixed) – 테이블 열 제목이 포함된 배열 또는 여러 문자열

반환:

CI_Table 인스턴스 (메소드 체이닝)

반환 형식:

CI_Table

테이블 제목을 설정할 수 있습니다. 배열 또는 개별 매개변수를 제출할 수 있습니다:

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

$this->table->set_heading(array('Name', 'Color', 'Size'));
add_row([$args = array()[, ...]])
매개변수:
  • $args (mixed) – 행 값이 포함된 배열 또는 여러 문자열

반환:

CI_Table 인스턴스 (메소드 체이닝)

반환 형식:

CI_Table

테이블에 행을 추가할 수 있습니다. 배열 또는 개별 매개변수를 제출할 수 있습니다:

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

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

개별 셀의 태그 속성을 설정하려면 해당 셀에 연관 배열을 사용할 수 있습니다. 연관 키 data는 셀의 데이터를 정의합니다. 다른 모든 key => val 쌍은 태그에 key=’val’ 속성으로 추가됩니다:

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

// 생성 결과
// <td class='highlight' colspan='2'>Blue</td><td>Red</td><td>Green</td>
make_columns([$array = array()[, $col_limit = 0]])
매개변수:
  • $array (array) – 여러 행의 데이터가 포함된 배열

  • $col_limit (int) – 테이블의 열 수

반환:

HTML 테이블 열 배열

반환 형식:

array

이 메소드는 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);

// 다음 프로토타입으로 테이블 생성

<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>
set_template($template)
매개변수:
  • $template (array) – 템플릿 값이 포함된 연관 배열

반환:

성공 시 TRUE, 실패 시 FALSE

반환 형식:

bool

템플릿을 설정할 수 있습니다. 전체 또는 부분 템플릿을 제출할 수 있습니다:

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

$this->table->set_template($template);
set_empty($value)
매개변수:
  • $value (mixed) – 빈 셀에 넣을 값

반환:

CI_Table 인스턴스 (메소드 체이닝)

반환 형식:

CI_Table

비어 있는 테이블 셀에 사용할 기본값을 설정할 수 있습니다. 예를 들어 줄 바꿈 없는 공백을 설정할 수 있습니다:

$this->table->set_empty("&nbsp;");
clear()
반환:

CI_Table 인스턴스 (메소드 체이닝)

반환 형식:

CI_Table

테이블 제목, 행 데이터 및 캡션을 지울 수 있습니다. 다른 데이터로 여러 테이블을 표시해야 하는 경우 각 테이블이 생성된 후 이 메소드를 호출하여 이전 테이블 정보를 지워야 합니다.

예시:

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

$this->table->set_caption('Preferences');
$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_caption('Shipping');
$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();