Page 490 - Beginning PHP 5.3
P. 490

Part III: Using PHP in Practice

                     Method                               Description
                       setAutoFill( $ fill , $ body  )       This function tells  HTML_Table  to pre - populate
                                                        newly created empty table cells with a string value.
                                                          $ fill  is the string to insert into empty cells when

                                                        they ’ re created. The optional   $ body   argument works
                                                        like its counterpart in   addRow() .
                       toHtml()                             Returns the HTML markup to display the table. Call
                                                        this method once you ’ ve created your table to
                                                        retrieve the HTML for inserting into the Web page.

                      You can find a complete list of   HTML_Table  methods at  http://pear.php.net/manual/en/
                    package.html.html-table.php .
                   To create a table, you first create a new   HTML_Table  object to store the table data and other settings, then
                call various methods of that object to add data cells, format the table, and so on. When you ’ re done, call
                the object ’ s   toHtml()  method to retrieve the markup for displaying the table.



              Try It Out     Displaying Fibonacci Numbers with HTML_Table
                Chapter 4 featured a script that used looping to display the first few numbers of the Fibonacci
                sequence. The numbers were displayed in an HTML table by outputting the HTML markup for the
                table directly.
                In this example, you rewrite this script to use HTML_Table to generate the markup. Save the following
                script as fibonacci2.php in your document root folder.

                    <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                     “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                    <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                      <head>
                        <title>Fibonacci sequence using HTML_Table</title>
                        <link rel=”stylesheet” type=”text/css” href=”common.css” />
                        <style type=”text/css”>
                          th { text-align: left; background-color: #999; }
                          th, td { padding: 0.4em; }
                          tr.alt td { background: #ddd; }
                        </style>
                      </head>
                      <body>

                        <h2>Fibonacci sequence using HTML_Table</h2>

                    <?php

                    require_once( “HTML/Table.php” );
                    $attrs = array( “cellspacing” => 0, “border” => 0, “style” => “width: 20em;
                    border: 1px solid #666;” );
                    $table = new HTML_Table( $attrs );


              452





                                                                                                      9/21/09   9:14:50 AM
          c15.indd   452                                                                              9/21/09   9:14:50 AM
          c15.indd   452
   485   486   487   488   489   490   491   492   493   494   495