Accessing Databases with ADODB - Generating HTML
(Page 6 of 12 )
ADODB makes it very easy to perform common HTML generation tasks with information retrieved from a database. These tasks include displaying a record set in an HTML table, printing <select> tags containing data from a record set, and splitting data from a record set across multiple pages.
Displaying a Record Set in an HTML Table The rs2html() function produces an HTML table of results with just one function call. The function is defined in the adodb/tohtml.inc.php file, so you must include or require that file to use rs2html(). Just pass a record set to rs2html(), and it prints an HTML table:
require 'adodb/tohtml.inc.php';
// Assume $conn is a valid database connection
$rs = $conn->execute('SELECT flavor,calories,price FROM ice_cream');
rs2html($rs);
This prints the following HTML:
<table COLS=3 BORDER='1' WIDTH='98%'>
<TH>flavor</TH><TH>calories</TH><TH>price</TH>
<TR valign=top>
<TD>Chocolate </TD>
<TD align=right>10 </TD>
<TD align=right>4.50 </TD>
</TR>
<TR valign=top>
<TD>Vanilla </TD>
<TD align=right>20 </TD>
<TD align=right>4.50 </TD>
</TR>
<TR valign=top>
<TD>"Heavenly" Hash </TD>
<TD align=right>60 </TD>
<TD align=right>5.95 </TD>
</TR>
<TR valign=top>
<TD>Diet Cardboard </TD>
<TD align=right>0 </TD>
<TD align=right>1.15 </TD>
</TR>
</table>
Figure 2-1 shows a rendered version of the HTML table.

Figure 2-1. The rs2html() function generates an HTML table.
By default, the table produced by rs2html() has BORDER='1' WIDTH='98%' as the attributes for the <table> tag, but you can override those defaults by passing different attributes as a second argument to rs2html(). Using the record set from the previous example, rs2html($rs,'CLASS="fancytable"') prints a table whose opening tag is as follows:
<table COLS=3 CLASS="fancytable">
You can also change the text of the table’s header row. By default, rs2html() uses the column names in the result set, but if an array of strings is passed as the third argument to rs2html(), it uses the values in that array instead. Using the record set from the previous example, rs2html($rs,false,array('Ice Cream Flavor','Calorie Count','Cost')) prints a table whose header row is as follows:
<TH>Ice Cream Flavor</TH>
<TH>Calorie Count</TH><TH>Cost</TH>
By passing false as a second argument to rs2html() in this example, you tell the function to use the defaults for <table> tag attributes.
The fourth argument to rs2html() is a boolean that controls whether string values in the table have their HTML entities encoded by htmlentities(). It defaults to true. If you don’t want those values to have their entities encoded, pass false. This doesn’t affect entity encoding of the values in the table header row. When rs2html() uses the column names from the result set, it always encodes their HTML entities. When it uses column names passed in as its third argument, it never encodes their HTML entities.
This is from Essential PHP Tools, by David Sklar (Apress, ISBN 1590592808). Check it out at your favorite bookstore today. Buy this book now. |
Next: More HTML Generation >>
More Database Articles
More By Apress Publishing