Accessing Databases with ADODB - More HTML Generation
(Page 7 of 12 )
The fifth argument to rs2html() is a boolean that controls whether the function prints data or returns data. The default, true, means that rs2html() prints an HTML table. Passing false for this argument tells rs2html() to return the HTML table as a string instead.
The behavior of rs2html() is also controlled by two global variables: $gSQLMaxRows and $gSQLBlockRows. The $gSQLMaxRows variable controls how many rows from one record set rs2html() prints. It defaults to 1000. The $gSQLBlockRows variable controls how many rows from one record set rs2html() prints in one table. It defaults to 20. If a record set contains more than $gSQLBlockRows rows, rs2html() splits the records into multiple tables. The first $gSQLBlockRows rows go in the first table, the next $gSQLBlockRows rows go into the next table, and so on. You can set $gSQLBlockRows to 2 to see this at work with the ice cream example:
$rs = $conn->execute('SELECT flavor,calories,price FROM ice_cream');
$gSQLBlockRows = 2;
rs2html($rs);
The following is the HTML output. Figure 2-2 shows what it looks like in a browser.
<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>
</table>
<table COLS=3 BORDER='1' WIDTH='98%'>
<TH>flavor</TH><TH>calories</TH><TH>price</TH><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-2. The rs2html() function can generate multiple tables per page.
After putting two rows in the first table, rs2html() closes the table and starts a new one, complete with header row. The remaining two rows are put in the second table. Splitting large record sets into multiple tables can help browsers render the data quicker because a browser can’t render a table until it receives all the data in the table. It can also be helpful for users to see the header row interspersed periodically throughout a large data set so they know what each column means.
The rs2html() function prints rows from the record set starting from the current internal pointer position in the record set. If you’ve moved the internal pointer, by calling MoveNext(), for example, you need to move it back to the beginning of the record set with MoveFirst() if you want rs2html() to print the entire record set. After rs2html() runs, it leaves the internal pointer at the end of the record set, so you also need to use MoveFirst() if you want to call rs2html() twice on the same record set:
rs2html($rs);
$rs->MoveFirst();
rs2html($rs);
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: Printing >>
More Database Articles
More By Apress Publishing