How to output an html table from a php array _ PHP Tutorial

Source: Internet
Author: User
How to output an html table from a php array. Copy the code as follows :? Phpclassxtable {private $ tit, $ arr, $ fons, $ sextra; publicfunction _ construct () {$ this-titarray (); stringswithtitlesforfirstrow $ this-arrarr The 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 [] = $ ;}
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 ("

% S
", $ 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 ()."

";
?>

The http://www.bkjia.com/PHPjc/825267.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825267.htmlTechArticle 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 = arr...

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.