非常教程

Codeigniter 3参考手册

HTML Table

HTML Table Class

Table类提供的功能使您能够从数组或数据库结果集中自动生成HTML表格。

  • 使用表类
    • 初始化类
    • 例子
    • 改变你的表的外观
  • 类参考

使用表类

初始化类

像CodeIgniter中的大多数其他类一样,Table类在您的控制器中使用以下$this->load->library()方法进行初始化:

$this->load->library('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);

以下是从数据库查询结果创建的表的示例。表类将根据表名称自动生成标题(或者您可以使用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);

注意

您会注意到模板中有两组“行”块。这些允许您创建交替的行颜色或与行数据的每次迭代交替的设计元素。

您无需提交完整的模板。如果您只需要更改部分布局,则可以简单地提交这些元素。在这个例子中,只有表格打开标签正在改变:

$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表格

返回类型:

  • $ table_datamixed) - 用来填充表格行的数据
Returns:  HTML table
Return type:  string
返回包含生成表的字符串。接受可选参数,该参数可以是数组或数据库结果对象。

set_caption($caption)

参数:

$ caption(字符串) - 表格标题

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

  • $ caption字符串) - 表格标题
Returns:  CI\_Table instance (method chaining)
Return type:  CI\_Table
允许您为表格添加标题。

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

set_heading([$args = array()[, ...]])

参数:

$ args(mixed) - 包含表列标题的数组或多个字符串

返回:

CI_Table实例(方法链接)

返回类型:

CI_Table

  • $ argsmixed) - 包含表列标题的数组或多个字符串
Returns:  CI\_Table instance (method chaining)
Return type:  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

  • $ argsmixed) - 包含行值的数组或多个字符串
Returns:  CI\_Table instance (method chaining)
Return type:  CI\_Table
允许您向表中添加一行。您可以提交一个数组或离散参数:

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

如果您想要设置单个单元格的标签属性,则可以为该单元格使用关联数组。关联密钥数据定义了单元的数据。任何其他键=> val对都会添加为标签的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]])

参数:

$ array(array) - 包含多行数据的数组$ col_limit(int) - 表中列的数量

返回:

一个HTML表格列的数组

返回类型:

排列

  • $ arrayarray) - 一个包含多行数据的数组
  • $ col_limitint) - 表中列的数量
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)

参数:

$ template(array) - 一个包含模板值的关联数组

返回:

成功为TRUE,失败为FALSE

返回类型:

布尔

  • $ templatearray) - 一个包含模板值的关联数组
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)

参数:

$ value(混合) - 将值放入空单元格中

返回:

CI_Table实例(方法链接)

返回类型:

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实例(方法链接)

返回类型:

CI_Table

HTML Table相关

Codeigniter 3

CodeIgniter 是一个PHP MVC框架,特点是超轻量级、有数据加密、有灵活URI路由等。对于 PHP 程序员来说,它小巧但功能强大。

主页 https://codeigniter.com/
源码 https://github.com/bcit-ci/CodeIgniter
版本 3
发布版本 3.1.5