Accessing Databases with ADODB - Splitting Data Across Multiple Pages
(Page 9 of 12 )
The ADODB_Pager class, defined in the adodb-pager.inc.php file, provides a class that formats and displays a certain number of the rows in a record set with appropriate links to display more of the record set on other pages. You provide a database connection and a query when creating an ADODB_Pager object. The Render() method displays the results, shown in Figure 2-3.
$pager = new ADODB_Pager($conn,"SELECT _
id,flavor,calories,price FROM _
ice_cream_big");
$pager->Render();

Figure 2-3. ADODB_Pager displays a record set with pagination links.
The data is displayed in a table with formatting and navigation links. The leftmost link in the table goes to the first page, the second link goes to the previous page, the third link goes to the next page, and the rightmost link goes to the last page of data. The links contain a variable that controls what page is displayed, but they don’t automatically include other GET or POST variables that may have been set. By default, ADODB_Pager displays ten rows per page. You can override that by passing an argument to Render() with the number of rows per page you want. The following code generates a five-row table, shown in Figure 2-4:
$pager = new ADODB_Pager($conn,"SELECT id,flavor, _
calories,price FROM ice_cream_big");
$pager->Render(5);

Figure 2-4. ADODB_Pager can display a specified number of rows from a record set.
Internally, ADODB_Pager uses the rs2html() function to generate the HTML for the table. It overwrites the global variable $gSQLBlockRows with the number of rows per page it is displaying.
If you put more than one pager object in a page, ADODB_Pager needs to tell them apart so the next and previous links advance the correct pager. To accomplish this, each pager object needs a unique ID. The ID is specified as the third argument to the ADODB_Pager constructor. The default is adodb. To use two pager objects in one page, specify different IDs when you create the objects. The following code creates one pager with five rows and one with two rows:
$pager1 = new ADODB_Pager($conn,"SELECT id,flavor,calories, _
price FROM ice_cream_big",'big');
$pager1->Render(5);
$pager2 = new ADODB_Pager($conn,"SELECT id,flavor,calories, _
price FROM ice_cream", 'little');
$pager2->Render(2);
Figure 2-5 shows the two pager tables.

Figure 2-5. ADODB_Pager can display multiple tables on a page.
You can also include direct links to individual pages in the navigation by passing true as the fourth argument to the ADODB_Pager constructor. The following code creates a three-row table with individual page links:
$pager = new ADODB_Pager($conn,"SELECT id,flavor, _
calories,price FROM ice_cream_big",'big',true);
$pager->Render(3);
Figure 2-6 shows the table.

Figure 2-6. ADODB_Pager can display navigation links to individual pages in the record set.
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: Pivot Tables >>
More Database Articles
More By Apress Publishing