设为首页 收藏本站
查看: 735|回复: 0

[经验分享] [PHP]CodeIgniter学习手册(六):HTML表格类

[复制链接]

尚未签到

发表于 2017-4-10 12:19:27 | 显示全部楼层 |阅读模式
表格类提供了多个函数,允许你从数组或者数据库结果集中自动生成HTML表格。
  

  初始化类

像CodeIgniter的其它类一样, 在控制器中使用$this->load->library 函数来初始化表格类:
$this->load->library('table');

  一旦被加载,可以这样建立一个表格库对象的实例: $this->table
  


例子
  此例演示如何通过一个多维数组(multi-dimensional array)自动生成表格。

  注意:数组的第一个索引将成为表头(或者你可以通过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);
  效果图:
DSC0000.png

  

  

  下面是一个由数据库查询结构创建而成的表格例子。
  表格类会基于表格的名字自动地生成表格标题(参考下面记述的函数,你可以使用set_heading()函数设置你自己的标题)。

$this->load->library('table');
$query = $this->db->query("SELECT * FROM my_table");
echo $this->table->generate($query);

  效果截图:
DSC0001.png

  

  


此例演示了如何使用连续的参数创建一个表格:
$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();


修改表格的外观

表格类允许你以你指定的设计编排,去设置表格模板。这里是模板的原型:

$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"块设置项。 这是允许你创建隔行颜色,或者设计每行数据的重复间隔元素。


你不必提交全部的模板。如果你只想改变编排的一部分,你可以简单地提交那部分的元素。在这个例子里,只有表格的开始标签被更改:

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


  函数参考:
  


$this->table->generate()

返回一个包含生成的表格的字符串。 接受一个可选的参数,该参数可以是一个数组或是从数据库获取的结果对象。
  

  $this->table->set_caption()

允许你给表格添加一个标题

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


  


$this->table->set_heading()

允许你设置表格的表头。你可以提交一个数组或分开的参数:
$this->table->set_heading('Name', 'Color', 'Size');
$this->table->set_heading(array('Name', 'Color', 'Size'));



  

  

  $this->table->add_row()

允许你在你的表格中添加一行。你可以提交一个数组或分开的参数:
$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>



  

  $this->table->make_columns()

这个函数以一个一维数组为输入,创建一个二维数组,它的深度和列数一样。这个函数可以把一个带有多个元素的单一数组根据表格的列数进行整理并显示。参考下面的例子:
$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);
生成的HTML代码如下:
<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()

使你能设置一个默认值,用来显示在表格中内容为空的单元格。 例如,你可以设置一个non-breaking space(用来防止表格边框破损的空格):
$this->table->set_empty("");
$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方法或一个有效的方法应用到所有的单元格中的数据的数组对象。
$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()方法实现html转义,其结果如下:
<td>Fred</td>
<td><strong>Blue</strong></td>
<td>Small</td>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-362933-1-1.html 上篇帖子: php学习笔记(五)字符串及其相关处理函数 下篇帖子: PHP,C# 和JAVARSA签名及验签
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表