ホームページ  >  記事  >  バックエンド開発  >  PHP配列をHTMLテーブルに出力する方法_PHPチュートリアル

PHP配列をHTMLテーブルに出力する方法_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 10:24:571114ブラウズ

复制代码代码如下:

class xtable
{
private $tit,$arr,$fons,$sextra;
public function __construct()
{
$this-> ;tit=配列();       // 最初の行にタイトルを含む文字列
$this->arr=array();       // セルに表示するデータ
$this->fons=array("#EEEEEE","#CCEEEE");  // 奇数行と偶数行の背景色
$this->sextra="";       // テーブル タグの HTML コードを追加します
}

public function extra($s) // タグ テーブルの HTML コードを追加します
{
$this->sextra=$s;
}
public function background($ arr) {if (is_array($arr)) $this->fons=$arr; else $this->fons=array($arr,$arr);}
パブリック関数タイトル($text,$) {$this->tit=$text; $this->sesttit=$style;}
public function addrow($a) {$this->arr[]=$a;}
public function addrows($arr) {$n=count($arr) ; for($i=0;$iaddrow($arr[$i]);}
public function html()
{
$cfondos=$this->fons ;
$titulos="";
$t=count($this->tit);
for($k=0;$k<$t;$k++)
{
$titulos.= sprintf("%s",$this->tit[$k]);
}
$titulos.="";

$celdas="";
$n=count($this->arr);
for($i=0;$i<$n;$i++)
{
$celdas.=sprintf("",$this->fons[$i%2]);
$linea=$this->arr[$i];
$m=count($linea);
for( $j=0;$j $celdas.=sprintf("%s","",$linea[$j]);
$celdas .="";
}
return sprintf("%s%s
",$this ->sextra,$titulos,$celdas);
}
public function example()
{
$tit=array("Apellidos","Nombre","Telefono");
$r1=array("ガルシア","イワン","888");
$r2=array("マルコ","アルフォンソ","555");
$x=new xtable();
$x->タイトル($tit);      //タイトルを取得 array
$x->addrows(array($r1,$r2));   // すべての行を同時に取得します
return $x->html();     //取得/表示/保存するための HTML コードを返します
}
}


// Example
$t1=new xtable();
echo $t1->example()."


";

$t2=new xtable();
for($i=1;$i {
$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; 背景色:シアン; カラー:ネイビー;'");
echo $t2->html ()."


";

$t3=new xtable();
for($i=1;$i {
$t3->addrow(array("5x".$i,5*$i) );

}
$t3->background(array("olive","maroon"));
$t3->titles(array("九九","5"));
$t3- >extra("style='border:点線赤 10px;padding-left:4px;padding-right:4px;text-align:right;width:500px;background-color:black; color:white;'");
echo $t3->html()."


";

$t4=new xtable();
$a=array("#");
for($i=1;$i {
$a[]=$i;
}
$t4->addrow($a);
$t4->background(array("pink","gold"));
$tit=array(); $tit[]="数字";
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:シアン; color:navy;'") ;
echo $t4->html()."


";
?>

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/825267.html技術記事例: ?php class xtable { private $tit,$arr,$fons,$sextra; public function __construct() { $this-tit=array();// 最初の行にタイトルを含む文字列 $this-arr=arr...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。