Page 435 - Beginning PHP 5.3
P. 435
Chapter 13: Retrieving Data from MySQL with PHP
& nbsp;
< ?php if ( $start + PAGE_SIZE < $totalRows ) { ? >
< a href=”view_members.php?start= < ?php echo min( $start + PAGE_SIZE,
$totalRows ) ? > & amp;order= < ?php echo $order ? > ” > Next page < /a >
< ?php } ? >
< /div >
< ?php
displayPageFooter();
? >
This script makes use of the Member class to retrieve the list of members from the database, then displays
the list in the page. It starts by retrieving two query string parameters — start , representing the record
position from which to start displaying the records in the page, and order , which specifies which
column to sort the data by — and storing the parameter values in two variables, $start and $order . If
either parameter wasn ’ t passed to the script, a default value is used. To improve security, the script filters
both parameters to make sure they contain valid values: $start is cast to int , whereas $order uses a
regular expression to remove any non - alphabetic characters (because only letters are used for the field
names in the members table).
You can find out more about regular expressions in Chapter 18.
Next, the script retrieves the list of members to display in the page. The code to do this is really simple,
because all the complexity is nicely hidden away in the Member class:
list( $members, $totalRows ) = Member::getMembers( $start, PAGE_SIZE, $order );
Member::getMembers() is called, passing in the row to start retrieving records from, the number of
records to retrieve, and the string to use for the ORDER BY clause. getMembers() dutifully returns a
two - element array. The first element — the list of Member objects — is stored in $members , and the
second element — the total number of members in the members table — is stored in $totalRows .
Now that the data is retrieved, it ’ s simply a case of displaying the list in the page. First,
displayPageHeader() is called with an appropriate page title to display the XHTML header. Then an
XHTML table element is started, with three header cells for username, first name, and last name:
< th > < ?php if ( $order != “username” ) { ? > < a
href=”view_members.php?order=username” > < ?php } ? > Username < ?php if
( $order != “username” ) { ? > < /a > < ?php } ? > < /th >
< th > < ?php if ( $order != “firstName” ) { ? > < a href=”view_members.php?
order=firstName” > < ?php } ? > First name < ?php if ( $order != “firstName” )
{ ? > < /a > < ?php } ? > < /th >
< th > < ?php if ( $order != “lastName” ) { ? > < a href=”view_members.php?
order=lastName” > < ?php } ? > Last name < ?php if ( $order != “lastName” )
{ ? > < /a > < ?php } ? > < /th >
In each case, the column name is linked to a URL that, when visited, runs view_members.php again
with a new order query parameter to sort the data by that column. However, if the data is already
sorted by a particular column, that column ’ s name isn ’ t linked, in order to indicate that the data is sorted
by that column.
397
9/21/09 9:12:06 AM
c13.indd 397
c13.indd 397 9/21/09 9:12:06 AM