Page 311 - Beginning PHP 5.3
P. 311
Chapter 10: Preserving State With Query Strings
$start = 0;
if ( isset( $_GET[“start”] ) and $_GET[“start”] > = 0 and $_GET[“start”] < =
1000000 ) {
$start = (int) $_GET[“start”];
}
Next, the script works out the last integer to display on the current page, and stores it in another
variable, $end :
$end = $start + PAGE_SIZE - 1;
Now it ’ s simply a case of displaying the table of ten integers, along with their squares. PHP ’ s pow()
function is used to calculate the square of each integer:
? >
< h2 > Number squaring < /h2 >
< p > Displaying the squares of the numbers < ?php echo $start ? > to < ?php echo
$end ? > : < /p >
< table cellspacing=”0” border=”0” style=”width: 20em; border: 1px solid
#666;” >
< tr >
< th > n < /th >
< th > n < sup > 2 < /sup > < /th >
< /tr >
< ?php
for ( $i=$start; $i < = $end; $i++ )
{
? >
< tr < ?php if ( $i % 2 != 0 ) echo ‘ class=”alt”’ ? > >
< td > < ?php echo $i? > < /td >
< td > < ?php echo pow( $i, 2 )? > < /td >
< /tr >
< ?php
}
? >
< /table >
Finally, the Next Page and (if appropriate) Previous Page links are displayed. Notice how the script
builds the query string within each link:
< p >
< ?php if ( $start > 0 ) { ? >
< a href=”number_squaring.php?start= < ?php echo $start - PAGE_SIZE
? > ” > < Previous Page < /a > |
< ?php } ? >
< a href=”number_squaring.php?start= < ?php echo $start + PAGE_SIZE ? > ” > Next
Page >< /a >
< /p >
273
9/21/09 9:05:09 AM
c10.indd 273
c10.indd 273 9/21/09 9:05:09 AM