How to output an html table from a php Array
This article mainly introduces how to output html tables from php arrays. For more information, see
The Code is as follows:
<? 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 [] = $ ;}
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 </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 (). "
$ 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 (). "
$ 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 (). "
$ T4 = new xtable ();
$ A = array ("#");
For ($ I = 1; $ I <= 10; $ I ++)
{
$ A [] = $ I;
}
$ T4-> addrow ($ );
$ 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 (). "
?>