11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 35 • PRACTICAL DATABASE QUERIESwww.it-ebooks.info// Create the 'previous' linkif ($recordstart > 0) {$prev = $recordstart - $pagesize;$url = $_SERVER['<strong>PHP</strong>_SELF']."?recordstart=$prev";printf("Previous Page", $url);}// Create the 'next' linkif ($totalrows > ($recordstart + $pagesize)) {$next = $recordstart + $pagesize;$url = $_SERVER['<strong>PHP</strong>_SELF']."?recordstart=$next";printf("Next Page", $url);}Sample output is shown in Figure 35-5.Figure 35-5. Creating paged results (four results per page)Listing Page NumbersIf you have several pages of results, the user might wish to traverse them in a nonlinear order. Forexample, the user might choose to jump from page one to page three, then page six, then back to pageone again. Happily, providing users with a linked list of page numbers is surprisingly easy. Building onthe previous example, you start by determining the total number of pages and assigning that value to$totalpages. You determine the total number of pages by dividing the total result rows by the chosenpage size, and round upward using the ceil() function:$totalpages = ceil($totalrows / $pagesize);Next, you determine the current page number, and assign it to $currentpage. You determine thecurrent page by dividing the present record offset ($recordstart) by the chosen page size ($pagesize)and adding one to account for the fact that LIMIT offsets start with 0:$currentpage = ($recordstart / $pagesize ) + 1;Next, create a function titled pageLinks(), and pass it the following four parameters:• $totalpages: The total number of result pages, stored in the $totalpages variable.• $currentpage: The current page, stored in the $currentpage variable.682

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!