How to output an HTML table from a PHP array

Source: Internet
Author: User

  This article mainly introduces the method of output HTML table of PHP array, need friends can refer to the following

    Code as follows: <?php class Xtable { private $tit, $arr, $fons, $sextra;  public function __construct () &NB Sp {  $this->tit=array ()      //strings with titles for 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 &nbsp}    public function extra ($s)      / /Add some HTML code for the tag table  {  $this->sextra= $s; &nbsp}  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); &NBSP}  public Function Example ()  {  $tit =array ("Apellidos", "Nombre", "telefono");    $r 1 = Array ("Garcia", "Ivan", "8;    $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 (). <hr/> ";   $t 2=new xtable (); For ($i =1 $i <=10; $i +=2)  {  $t 2->addrow (Array ("ODD", $i))   $t 2->addrow (Array ("even", $i + 1)) ; &NBSP} $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 (). <hr/> ";   $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 (). <hr/> ";   $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 (). <hr/> ";?>  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.