Copy CodeThe code is as follows:
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 = "";
$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 < $m; $j + +)
$celdas. =sprintf ("%s"," ", $linea [$j]);
$celdas. = "";
}
Return sprintf ("
", $this->sextra, $titulos, $celdas);
}
Public Function Example ()
{
$tit =array ("Apellidos", "Nombre", "telefono");
$r 1=array ("Garcia", "Ivan", "888");
$r 2=array ("Marco", "Alfonso", "555");
$x =new xtable ();
$x->titles ($tit); Take titles Array
$x->addrows (Array ($r 1, $r 2)); Take all rows at same time
return $x->html (); Return HTML code to get/show/save it
}
}
Example
$t 1=new xtable ();
echo $t 1->example (). "
";
$t 2=new xtable ();
for ($i =1; $i <=10; $i +=2)
{
$t 2->addrow (Array ("ODD", $i));
$t 2->addrow (Array ("even", $i + 1));
}
$t 2->background (Array ("Pink", "gold"));
$t 2->titles (Array ("TYPE", "#");
$t 2->extra ("style= ' width:500px; Background-color:cyan; Color:navy; ' ");
echo $t 2->html (). "
";
$t 3=new xtable ();
for ($i =1; $i <=6; $i + +)
{
$t 3->addrow (Array ("5x". $i, 5* $i));
}
$t 3->background (Array ("Olive", "maroon"));
$t 3->titles (Array ("Multiplication table", "5"));
$t 3->extra ("style= ' border:dotted red 10px; padding-left:4px;padding-right:4px; text-align:right;width:500px; Background-color:black; Color:white; ' ");
echo $t 3->html (). "
";
$t 4=new xtable ();
$a =array ("#");
for ($i =1; $i <=10; $i + +)
{
$a []= $i;
}
$t 4->addrow ($a);
$t 4->background (Array ("Pink", "gold"));
$tit =array (); $tit []= "Numbers";
for ($i =1; $i <=10; $i + +) $tit []= "#";
$t 4->titles ($tit);
$t 4->extra ("style= ' border:solid 1px silver; padding-left:4px;padding-right:4px; text-align:center;width:500px; Background-color:cyan; Color:navy; ' ");
echo $t 4->html (). "
";
?>