将php数组输出html表格的方法


Posted in PHP onFebruary 24, 2014
<?php
class xtable
{
 private $tit,$arr,$fons,$sextra;
 public function __construct()
 {
  $this->tit=array();       // strings with titles for first row 
  $this->arr=array();       // data to show on cells
  $this->fons=array("#EEEEEE","#CCEEEE");  // background colors for odd and even rows
  $this->sextra="";       // extra html code for table tag
 } public function extra($s)      // add some html code for the tag table
 {
  $this->sextra=$s;
 }
 public function background($arr) {if (is_array($arr)) $this->fons=$arr; else $this->fons=array($arr,$arr);}
 public function titles($text,$style="") {$this->tit=$text; $this->sesttit=$style;}
 public function addrow($a) {$this->arr[]=$a;}
 public function addrows($arr) {$n=count($arr); for($i=0;$i<$n;$i++) $this->addrow($arr[$i]);}
 public function html()
 {
  $cfondos=$this->fons;
  $titulos="<tr>";
  $t=count($this->tit);
  for($k=0;$k<$t;$k++)
  {
   $titulos.=sprintf("<th>%s</th>",$this->tit[$k]);
  }
  $titulos.="</tr>";
  $celdas="";
  $n=count($this->arr);
  for($i=0;$i<$n;$i++)
  {
   $celdas.=sprintf("<tr style='background-color:%s'>",$this->fons[$i%2]);
   $linea=$this->arr[$i];
   $m=count($linea);
   for($j=0;$j<$m;$j++)
    $celdas.=sprintf("<td  %s>%s</td>","",$linea[$j]);
   $celdas.="</tr>";
  }
  return sprintf("<table cellpadding='0' cellspacing='0' border='1' %s>%s%s</table>",$this->sextra,$titulos,$celdas);
 }
 public function example()
 {
  $tit=array("Apellidos","Nombre","Telefono"); 
  $r1=array("Garcia","Ivan","888"); 
  $r2=array("Marco","Alfonso","555"); 
  $x=new xtable(); 
  $x->titles($tit);      //take titles array
  $x->addrows(array($r1,$r2));   // take all rows at same time
  return $x->html();     //return html code to get/show/save it 
 }
}


// Example
$t1=new xtable();
echo $t1->example()."<hr />";
$t2=new xtable();
for($i=1;$i<=10;$i+=2)
 {
  $t2->addrow(array("ODD",$i));
  $t2->addrow(array("EVEN",$i+1));
 }
$t2->background(array("pink","gold"));
$t2->titles(array("TYPE","#"));
$t2->extra(" style='width:500px; background-color:cyan; color:navy;'");
echo $t2->html()."<hr />";
$t3=new xtable();
for($i=1;$i<=6;$i++)
 {
  $t3->addrow(array("5x".$i,5*$i));
 }
$t3->background(array("olive","maroon"));
$t3->titles(array("Multiplication table","5"));
$t3->extra("style='border:dotted red 10px; padding-left:4px;padding-right:4px; text-align:right;width:500px; background-color:black; color:white;'");
echo $t3->html()."<hr />";
$t4=new xtable();
$a=array("#");
for($i=1;$i<=10;$i++)
 {
  $a[]=$i;
 }
$t4->addrow($a);
$t4->background(array("pink","gold"));
$tit=array(); $tit[]="Numbers";
for($i=1;$i<=10;$i++) $tit[]="#";
$t4->titles($tit);
$t4->extra("style='border:solid 1px silver; padding-left:4px;padding-right:4px; text-align:center;width:500px; background-color:cyan; color:navy;'");
echo $t4->html()."<hr />";
?>
PHP 相关文章推荐
一个用php3编写的简单计数器
Oct 09 PHP
php入门教程 精简版
Dec 13 PHP
PHP中文URL编解码(urlencode()rawurlencode()
Jul 03 PHP
PHP代码网站如何防范SQL注入漏洞攻击建议分享
Mar 01 PHP
一个PHP并发访问实例代码
Sep 06 PHP
探讨如何把session存入数据库
Jun 07 PHP
PHP 过滤页面中的BOM(实现代码)
Jun 29 PHP
php简单实现无限分类树形列表的方法
Mar 27 PHP
表单提交错误后返回内容消失问题的解决方法(PHP网站)
Oct 20 PHP
PHP验证码类ValidateCode解析
Jan 07 PHP
phpstudy默认不支持64位php的解决方法
Feb 20 PHP
实现laravel 插入操作日志到数据库的方法
Oct 11 PHP
php格式化日期和时间格式化示例分享
Feb 24 #PHP
php目录操作实例代码
Feb 21 #PHP
php无限遍历目录示例
Feb 21 #PHP
php自动加载autoload机制示例分享
Feb 20 #PHP
php多文件上传下载示例分享
Feb 20 #PHP
php分页代码学习示例分享
Feb 20 #PHP
php多文件上传实现代码
Feb 20 #PHP
You might like
php学习之简单计算器实现代码
2011/06/09 PHP
php 解决旧系统 查出所有数据分页的类
2012/08/27 PHP
zend framework配置操作数据库实例分析
2012/12/06 PHP
PHP检测移动设备类mobile detection使用实例
2014/04/14 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
2015/03/19 PHP
简单了解WordPress开发中update_option()函数的用法
2016/01/11 PHP
win10环境PHP 7 安装配置【教程】
2016/05/09 PHP
php基于mcrypt_encrypt和mcrypt_decrypt实现字符串加密解密的方法
2016/07/12 PHP
详解php中serialize()和unserialize()函数
2017/07/08 PHP
Javascript Select操作大集合
2009/05/26 Javascript
Javascript 类与静态类的实现
2010/04/01 Javascript
jquery中常用的函数和属性详细解析
2014/03/07 Javascript
JavaScript判断表单提交时哪个radio按钮被选中的方法
2015/03/21 Javascript
jQuery实现点击按钮弹出可关闭层的浮动层插件
2015/09/19 Javascript
JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
2016/02/25 Javascript
详解vue项目首页加载速度优化
2017/10/18 Javascript
微信小程序canvas.drawImage完全显示图片问题的解决
2018/11/30 Javascript
小程序自定义单页面、全局导航栏的实现代码
2019/03/15 Javascript
webpack打包优化的几个方法总结
2020/02/10 Javascript
Python中使用支持向量机SVM实践
2017/12/27 Python
pytorch cnn 识别手写的字实现自建图片数据
2018/05/20 Python
django框架基于模板 生成 excel(xls) 文件操作示例
2019/06/19 Python
使用Python中的reduce()函数求积的实例
2019/06/28 Python
Django中的静态文件管理过程解析
2019/08/01 Python
pycharm显示远程图片的实现
2019/11/04 Python
Python算法中的时间复杂度问题
2019/11/19 Python
基于Python检测动态物体颜色过程解析
2019/12/04 Python
世界最大的海报和艺术印刷商店:AllPosters.com
2017/02/01 全球购物
C++面试题:关于链表和指针
2013/06/05 面试题
《美丽的田园》教学反思
2014/03/01 职场文书
企业年会主持词
2014/03/27 职场文书
石油工程专业毕业生求职信
2014/04/13 职场文书
毕业生应聘求职信
2014/07/10 职场文书
云南省召开党的群众路线教育实践活动总结会议新闻稿
2014/10/21 职场文书
导游词开场白
2015/01/31 职场文书
一级电子管军用接收机测评
2022/04/05 无线电