?
This document uses PHP Chinese website manual Release
Table類(lèi)提供的功能使您能夠從數(shù)組或數(shù)據(jù)庫(kù)結(jié)果集中自動(dòng)生成HTML表格。
使用表類(lèi)
初始化類(lèi)
例子
改變你的表的外觀
類(lèi)參考
像CodeIgniter中的大多數(shù)其他類(lèi)一樣,Table類(lèi)在您的控制器中使用以下$this->load->library()
方法進(jìn)行初始化:
$this->load->library('table');
加載后,表格庫(kù)對(duì)象將可用:
$this->table
下面是一個(gè)示例,展示如何從多維數(shù)組創(chuàng)建表。請(qǐng)注意,第一個(gè)數(shù)組索引將成為表格標(biāo)題(或者您可以使用set_heading()
下面函數(shù)參考中描述的方法設(shè)置您自己的標(biāo)題)。
$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);
以下是從數(shù)據(jù)庫(kù)查詢(xún)結(jié)果創(chuàng)建的表的示例。表類(lèi)將根據(jù)表名稱(chēng)自動(dòng)生成標(biāo)題(或者您可以使用set_heading()
下面的類(lèi)參考中描述的方法設(shè)置您自己的標(biāo)題)。
$this->load->library('table');$query = $this->db->query('SELECT * FROM my_table');echo $this->table->generate($query);
下面是一個(gè)示例,顯示如何使用離散參數(shù)創(chuàng)建表格:
$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();
這里是相同的例子,除了各個(gè)參數(shù)之外,還使用了數(shù)組:
$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類(lèi)允許您設(shè)置一個(gè)表格模板,您可以使用該模板指定布局的設(shè)計(jì)。這是模板原型:
$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);
注意
您會(huì)注意到模板中有兩組“行”塊。這些允許您創(chuàng)建交替的行顏色或與行數(shù)據(jù)的每次迭代交替的設(shè)計(jì)元素。
您無(wú)需提交完整的模板。如果您只需要更改部分布局,則可以簡(jiǎn)單地提交這些元素。在這個(gè)例子中,只有表格打開(kāi)標(biāo)簽正在改變:
$template = array( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">');$this->table->set_template($template);
您也可以在配置文件中為這些設(shè)置默認(rèn)值。
class CI_Table$function = NULL
允許您指定一個(gè)本機(jī)PHP函數(shù)或一個(gè)有效的函數(shù)數(shù)組對(duì)象以應(yīng)用于所有單元數(shù)據(jù)。
$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();
在上面的例子中,所有單元格數(shù)據(jù)都將通過(guò)PHP htmlspecialchars()
函數(shù)運(yùn)行,導(dǎo)致:
<td>Fred</td><td><strong>Blue</strong></td><td>Small</td>
generate([$table_data = NULL])
參數(shù): | $ table_data(mixed) - 用來(lái)填充表格行的數(shù)據(jù) |
---|---|
返回: | HTML表格 |
返回類(lèi)型: | 串 |
$ table_data(mixed) - 用來(lái)填充表格行的數(shù)據(jù)
Returns: HTML table
Return type: string
返回包含生成表的字符串。接受可選參數(shù),該參數(shù)可以是數(shù)組或數(shù)據(jù)庫(kù)結(jié)果對(duì)象。
set_caption($caption)
參數(shù): | $ caption(字符串) - 表格標(biāo)題 |
---|---|
返回: | CI_Table實(shí)例(方法鏈接) |
返回類(lèi)型: | CI_Table |
$ caption(字符串) - 表格標(biāo)題
Returns: CI\_Table instance (method chaining)
Return type: CI\_Table
允許您為表格添加標(biāo)題。
$this->table->set_caption('Colors');
set_heading([$args = array()[, ...]])
參數(shù): | $ args(mixed) - 包含表列標(biāo)題的數(shù)組或多個(gè)字符串 |
---|---|
返回: | CI_Table實(shí)例(方法鏈接) |
返回類(lèi)型: | CI_Table |
$ args(mixed) - 包含表列標(biāo)題的數(shù)組或多個(gè)字符串
Returns: CI\_Table instance (method chaining)
Return type: CI\_Table
允許您設(shè)置表格標(biāo)題。您可以提交一個(gè)數(shù)組或離散參數(shù):
$this->table->set_heading('Name', 'Color', 'Size'); $this->table->set_heading(array('Name', 'Color', 'Size'));
add_row([$args = array()[, ...]])
參數(shù): | $ args(mixed) - 包含行值的數(shù)組或多個(gè)字符串 |
---|---|
返回: | CI_Table實(shí)例(方法鏈接) |
返回類(lèi)型: | CI_Table |
$ args(mixed) - 包含行值的數(shù)組或多個(gè)字符串
Returns: CI\_Table instance (method chaining)
Return type: CI\_Table
允許您向表中添加一行。您可以提交一個(gè)數(shù)組或離散參數(shù):
$this->table->add_row('Blue', 'Red', 'Green'); $this->table->add_row(array('Blue', 'Red', 'Green'));
如果您想要設(shè)置單個(gè)單元格的標(biāo)簽屬性,則可以為該單元格使用關(guān)聯(lián)數(shù)組。關(guān)聯(lián)密鑰數(shù)據(jù)定義了單元的數(shù)據(jù)。任何其他鍵=> val對(duì)都會(huì)添加為標(biāo)簽的key ='val'屬性:
$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>
make_columns([$array = array()[, $col_limit = 0]])
參數(shù): | $ array(array) - 包含多行數(shù)據(jù)的數(shù)組$ col_limit(int) - 表中列的數(shù)量 |
---|---|
返回: | 一個(gè)HTML表格列的數(shù)組 |
返回類(lèi)型: | 排列 |
$ array(array) - 一個(gè)包含多行數(shù)據(jù)的數(shù)組
$ col_limit(int) - 表中列的數(shù)量
Returns: An array of HTML table columns
Return type: array
This method takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:
$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>
set_template($template)
參數(shù): | $ template(array) - 一個(gè)包含模板值的關(guān)聯(lián)數(shù)組 |
---|---|
返回: | 成功為T(mén)RUE,失敗為FALSE |
返回類(lèi)型: | 布爾 |
$ template(array) - 一個(gè)包含模板值的關(guān)聯(lián)數(shù)組
Returns: TRUE on success, FALSE on failure
Return type: bool
Permits you to set your template. You can submit a full or partial template.
$template = array( 'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' ); $this->table->set_template($template);
set_empty($value)
參數(shù): | $ value(混合) - 將值放入空單元格中 |
---|---|
返回: | CI_Table實(shí)例(方法鏈接) |
返回類(lèi)型: | CI_Table |
$ value(混合) - 將值放入空單元格中
Returns: CI\_Table instance (method chaining)
Return type: CI\_Table
Lets you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:
$this->table->set_empty(" ");
clear()
返回: | CI_Table實(shí)例(方法鏈接) |
---|---|
返回類(lèi)型: | CI_Table |