博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
将PHP数组输出为HTML表格
阅读量:4317 次
发布时间:2019-06-06

本文共 2770 字,大约阅读时间需要 9 分钟。

1. [代码][PHP]代码    
<?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,$) {$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 />";
 http://www.huiyi8.com/clxgt/
$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 />";

转载于:https://www.cnblogs.com/xkzy/p/3935398.html

你可能感兴趣的文章
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>
【Luogu】P2498拯救小云公主(spfa)
查看>>
如何获取网站icon
查看>>
几种排序写法
查看>>
java 多线程的应用场景
查看>>
dell support
查看>>
转:Maven项目编译后classes文件中没有dao的xml文件以及没有resources中的配置文件的问题解决...
查看>>
MTK android 设置里 "关于手机" 信息参数修改
查看>>
单变量微积分笔记6——线性近似和二阶近似
查看>>
补几天前的读书笔记
查看>>
HDU 1829/POJ 2492 A Bug's Life
查看>>
CKplayer:视频推荐和分享插件设置
查看>>
CentOS系统将UTC时间修改为CST时间
查看>>