将php数组输出html表格的方法
<?php
classxtable
{
private$tit,$arr,$fons,$sextra;
publicfunction__construct()
{
$this->tit=array(); //stringswithtitlesforfirstrow
$this->arr=array(); //datatoshowoncells
$this->fons=array("#EEEEEE","#CCEEEE"); //backgroundcolorsforoddandevenrows
$this->sextra=""; //extrahtmlcodefortabletag
}
publicfunctionextra($s) //addsomehtmlcodeforthetagtable
{
$this->sextra=$s;
}
publicfunctionbackground($arr){if(is_array($arr))$this->fons=$arr;else$this->fons=array($arr,$arr);}
publicfunctiontitles($text,$style=""){$this->tit=$text;$this->sesttit=$style;}
publicfunctionaddrow($a){$this->arr[]=$a;}
publicfunctionaddrows($arr){$n=count($arr);for($i=0;$i<$n;$i++)$this->addrow($arr[$i]);}
publicfunctionhtml()
{
$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("<trstyle="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>";
}
returnsprintf("<tablecellpadding="0"cellspacing="0"border="1"%s>%s%s</table>",$this->sextra,$titulos,$celdas);
}
publicfunctionexample()
{
$tit=array("Apellidos","Nombre","Telefono");
$r1=array("Garcia","Ivan","888");
$r2=array("Marco","Alfonso","555");
$x=newxtable();
$x->titles($tit); //taketitlesarray
$x->addrows(array($r1,$r2)); //takeallrowsatsametime
return$x->html(); //returnhtmlcodetoget/show/saveit
}
}
//Example
$t1=newxtable();
echo$t1->example()."<hr/>";
$t2=newxtable();
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=newxtable();
for($i=1;$i<=6;$i++)
{
$t3->addrow(array("5x".$i,5*$i));
}
$t3->background(array("olive","maroon"));
$t3->titles(array("Multiplicationtable","5"));
$t3->extra("style="border:dottedred10px;padding-left:4px;padding-right:4px;text-align:right;width:500px;background-color:black;color:white;"");
echo$t3->html()."<hr/>";
$t4=newxtable();
$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:solid1pxsilver;padding-left:4px;padding-right:4px;text-align:center;width:500px;background-color:cyan;color:navy;"");
echo$t4->html()."<hr/>";
?>
相关文章
- 【php】关闭PHP错误提示方法,防止错误信息泄露
- php 中输出控制器名称,Thinkphp5.0&&5.1怎么获取模块名控制器名方法名(权限配置中)
- 【笔记】PHP封装的不错的一个Curl方法
- Linux上运行PHP的简易方法(linux如何运行php)
- Linux切换PHP版本:快速而安全的方法(linux切换php版本)
- 版本实现Linux安装多个PHP版本的方法(linux安装多个php)
- PHP连接MySQL的实现方法(php怎么连接mysql)
- Linux下的PHP编程之旅(linux的php)
- 配置PHP实现MySQL数据库连接(php连接mysql配置)
- 实现PHP程序Redis连接池的简单方法(redis连接池php)
- PHP与MySQL结合使用实现关联查询的方法(php关联mysql查询)
- PHP轻松访问MySQL数据:使用简明易懂的方法获取数据(php获取mysql数据)
- 慢解决PHP连接MySQL慢的方法(php连接mysql时间)
- PHP读取Linux文件的方法(php读取linux文件)
- MySQL实现PHP数组存储的效果(mysql存储数组php)
- PHP连接MSSQL数据库发生错误解决方法(php mssql 错误)
- 不重新编译PHP为php增加openssl模块的方法
- ubuntu+php环境下的Memcached安装方法
- Linux下实现PHP多进程的方法分享
- 简单的方法让你的后台登录更加安全(php中加session验证)
- php去除换行符的方法小结(PHP_EOL变量的使用)
- PHP处理excelcvs表格的方法实例介绍
- PHP父类调用子类方法的代码例子
- php随机取mysql记录方法小结