您当前的位置: 首页 > 网站编程 > PHP教程 > 将php数组输出html表格的方法

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

作者:不详 来源:网络 发布时间: 2014-08-09 22:39 点击:
这篇文章主要介绍了将php数组输出html表格的方法,需要的朋友可以参考下 代码如下: ?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 s

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

  这篇文章主要介绍了将php数组输出html表格的方法,需要的朋友可以参考下

  代码如下:

  <?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数组输出html表格的方法"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4