<? include “SuperHTMLDef.php”; $s = new SuperHTML(“Creating Lists”); $s->buildTop(); $myArray =array( “alpha”, “beta”, “gamma”, “delta”); $s->h3(“build an ordered list”); $s->buildList($myArray, “ol”); $s->h3(“unordered lists are the default”); $s->buildList(array(“alpha”, “beta”, “gamma”, “delta”)); $s->h3(“specify list type”); $s->buildList($myArray, “ol type = ‘a’”); $s->buildBottom(); print $s->getPage(); ?> Building a Basic List I started the list.php code by creating an array called (cleverly enough) $myArray. The buildList() method requires two parameters, an array, and a list type. Then I invoke the function: $s->buildList($myArray, “ol”); The SuperHTML object responds by adding the following code to its internal repre- sentation of the page: <ol> <li>alpha</li> <li>beta</li> <li>gamma</li> <li>delta</li> </ol> As you can see, each array item is enclosed in <li></li> tags, and the entire array is encased in an <ol></ol> set with appropriate indentation. You’ll be much more willing to use arrays when you have an easy tool like this to display them. 238 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r 239 C h a p t e r 7 W r i t i n g P r o g r a m s w i t h O b j e c t s Building an Ad Hoc List Of course you don’t always want an ordered list. The next call to the buildList() method is different from the first version in two ways: $s->buildList(array(“alpha”, “beta”, “gamma”, “delta”)); • First, I built this one on-the-fly rather than using a predefined array. This is useful when you want to build a list quickly but don’t already have an array. Just put the list values in an ad hoc array before sending the array to the buildList() method. • Second, this call is different because of the lack of a list type. If I don’t indicate what type of list I want, SuperHTML is smart enough to guess and put a legal value in for me. This behavior is a good example of a principle called polymorphism , where an object can act differently in different situ- ations. (Of course the formal definition is a little more profound than that, but this is good enough for now.) You can probably guess that the default list type is unordered. Building More-Specialized Lists The buildList() method has one more trick up its sleeve. If you look back at the third list on the HTML output, it is written with a specific list type ( <ol type = ‘a’> </ol> ). I included the list attributes in the list type parameter, like this: $s->buildList($myArray, “ol type = ‘a’”); You can specify the list type complete with attributes for more flexible lists. Making Tables with SuperHTML All the SuperHTML features you’ve seen up to now are pretty handy, but the main thing I wanted was easy work with tables. You’ll frequently find yourself out- putting data in tables, and it can be confusing to switch from PHP to HTML syn- tax (especially when you add SQL to the mix, because then you’re thinking in three very different languages at once). I wanted some features that easily let you build HTML tables from PHP data structures. Figure 7.5 shows a program with this capability. The basic plan for building tables with SuperHTML is similar to the approach for making lists. However, tables are based on data structures more complex than lists, as you can see when you peruse the code. <? include “SuperHTMLDef.php”; $s = new SuperHTML(“Creating Tables”); $s->buildTop(); $s->h3(“build table from 2d array”); $myArray = array( array(“English”, “Spanish”, “Japanese”), array(“One”, “Uno”, “Ichi”), array(“Two”, “Dos”, “Nii”), array(“Three”, “Tres”, “San”) ); $s->buildTable($myArray); $s->h3(“build table row-by-row”); $s->startTable(3); $s->tRow(array(“English”, “Greek”), “th”); $s->tRow(array(“a”, “alpha”)); $s->tRow(array(“b”, “beta”)); $s->endTable(); $s->buildBottom(); print $s->getPage(); ?> 240 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 7.5 These tables were made automatically by the SuperHTML object. 241 C h a p t e r 7 W r i t i n g P r o g r a m s w i t h O b j e c t s Creating a Basic Table Early in this chapter I mentioned that PHP arrays and HTML lists are natural com- panions. Each row of an HTML table can be seen as a PHP array, and a table can be seen as an array of rows. An array of arrays is a two-dimension array. It shouldn’t surprise you that building a table from a two-dimension array is easy. After I created an array called $myArray, turning it into a table with one line of code was trivial: $s->buildTable($myArray); Creating a More-Complex Table The buildTable() method is really easy, but it isn’t flexible enough for all needs. Frequently (especially in database applications) I want to build the top of the table, a header row, a series of rows in a loop, and then close off the table. I decided to add a more powerful suite of table-creation methods. These make it possible to make more-sophisticated tables, like the second one on Table.php. The following code builds a table line-by-line: $s->startTable(3); $s->tRow(array(“English”, “Greek”), “th”); $s->tRow(array(“a”, “alpha”)); $s->tRow(array(“b”, “beta”)); $s->endTable(); The startTable() method creates the code that begins the table definition. The parameter indicates the table’s border width. If you don’t indicate a border width, it defaults to 1. (Gosh, polymorphism is wonderful!) It won’t surprise you that the end of the table is indicated by the endTable() method. The cool part of this approach is the tRow() method, which makes up the table body. This method can accept one or two parameters. The first parameter is an array of values that populates the row. Of course this can be an array variable or created on-the-fly (as in this example). The second tRow() parameter is cell type. The default type is td, but you can specify th if you want a header row. (I explain in the form.phpprogram coming up next how to make a column of headers.) Call the tRow() method once for each table row. In an actual program, this frequently happens inside some sort of loop. Creating Super Forms The SuperHTML object is useful, but if it is really going to be helpful it must easily build form elements such as textboxes and submit buttons. Most of these objects are reasonably easy to build, but I’ve always found dropdown menus (HTML select objects) tedious to program. The SuperHTML object has a powerful, flexible, and easy approach to building various form elements. The page featured in Figure 7.6 was produced using some special object features. The form program code looks a little more involved than some other examples, but it’s not any more difficult than anything you’ve already seen. First I give you the code in full, and then I break it apart and show you the features. <? include “SuperHTMLDef.php”; $s = new SuperHTML(“Working with Forms”); $s->buildTop(); $s->h3(“form elements”); $s->addText(“<form> \n”); $s->textbox(“userName”, “Joe”); $s->h3(“create select object from associative array”); 242 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 7.6 Forms are no problem for SuperHTML. . Basic Table Early in this chapter I mentioned that PHP arrays and HTML lists are natural com- panions. Each row of an HTML table can be seen as a PHP array, and a table can be seen as an array of. ‘a’”); $s->buildBottom(); print $s->getPage(); ?> Building a Basic List I started the list .php code by creating an array called (cleverly enough) $myArray. The buildList() method requires. You’ll frequently find yourself out- putting data in tables, and it can be confusing to switch from PHP to HTML syn- tax (especially when you add SQL to the mix, because then you’re thinking in three