Vous êtes sur la page 1sur 6

Pagination - what it is and how to do it By Tony Marston 5th January 2004 Intended Audience Prerequisites What is pagination?

How to do it - the easy way How to do it - the hard way Sample software Intended Audience This tutorial is intended for developers who wish to give their users the abilit y to step through a large number of database rows in manageable chunks instead o f the whole lot in one go. Prerequisites It is assumed that you already have basic knowledge of PHP and MySQL. The techni ques described in this document are written with MySQL in mind but can be applie d to any database that allows the LIMIT and OFFSET clauses on the SELECT stateme nt. What is pagination? If you have a form which allows the user to browse through the rows in a databas e table, what do you do if that table has hundreds or even thousands of rows? It would not be a good idea to show all those rows in a single form, instead you s hould split the database output into more manageable chunks or 'pages'. There ar e two things you must do: Decide on the maximum number of database rows that can be included in each p age. You may hard code this value, or (my preferred method) you can define it in a variable so that the value may be changed at runtime. You then need to inform the user that other 'pages' are available and provid e a mechanism whereby the user is able to select a different 'page' of details. I currently use a set of hyperlinks in a separate pagination area which looks li ke this: Pagination Area dialog-types-pagination (1K) This area tells the user which page is currently being viewed, the total num ber of pages available, and contains links to go either forwards or backwards th rough the available pages. The hyperlinks are displayed as simple text if there are no previous or next pages. Note that these hyperlinks do not put the word FI RST, PREV, NEXT or LAST as a parameter in the URL string - they all specify an a bsolute page number in the format pageno=n. How to do it - the easy way To include all the relevant functionality in a single script you should follow t he steps outlined below. 1. Obtain the required page number This code will obtain the required page number from the $_GET array. Note that i f it is not present it will default to 1. if (isset($_GET['pageno'])) { $pageno = $_GET['pageno'];

} else { $pageno = 1; } // if 2. Identify how many database rows are available This code will count how many rows will satisfy the current query. $query = "SELECT count(*) FROM table WHERE ..."; $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR); $query_data = mysql_fetch_row($result); $numrows = $query_data[0]; 3. Calculate number of $lastpage This code uses the values in $rows_per_page and $numrows in order to identify th e number of the last page. $rows_per_page = 15; $lastpage = ceil($numrows/$rows_per_page); 4. Ensure that $pageno is within range This code checks that the value of $pageno is an integer between 1 and $lastpage . $pageno = (int)$pageno; if ($pageno > $lastpage) { $pageno = $lastpage; } // if if ($pageno < 1) { $pageno = 1; } // if 5. Construct LIMIT clause This code will construct the LIMIT clause for the sql SELECT statement. $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; 6. Issue the database query Now we can issue the database qery and process the result. $query = "SELECT * FROM table $limit"; $result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR); ... process contents of $result ... 7. Construct pagination hyperlinks Finally we must construct the hyperlinks which will allow the user to select oth er pages. We will start with the links for any previous pages. if ($pageno == 1) { echo " FIRST PREV "; } else { echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> "; $prevpage = $pageno-1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> "; } // if

Next we inform the user of his current position in the sequence of available pag es. echo " ( Page $pageno of $lastpage ) "; This code will provide the links for any following pages. if ($pageno == $lastpage) { echo " NEXT LAST "; } else { $nextpage = $pageno+1; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> "; echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> "; } // if That's all there is to it. Easy peasy, lemon squeezy. How to do it - the hard way Rather than have all my presentation, business and data access code in a single script I prefer to use the 3 tier architecture as described in my article A Deve lopment Infrastructure for PHP. As an added complication all my HTML output is g enerated from XSL stylesheets which use XML data files produced by my PHP script s. The steps are exactly the same, but the code is slightly different. 1. Obtain the required page number If a page number is supplied in the $_GET array this code will pass it to the da tabase object. if (isset($_GET['page'])) { $dbobject->setPageNo($_GET['page']); } // if The object uses this code to receive the passed value. function setPageNo ($pageno) // this allows a particular page number to be selected { $this->pageno = abs((int)$pageno); } // setPageNo As I serialise my object in the $_SESSION array if a new value is not supplied t hen I use whatever value was available in the previous instance. 2. Identify how many database rows are available This code is the same as before. 3. Calculate number of $lastpage Here the result depends on the vaue held in $this->rows_per_page. This is set to a default value in the class constructor, but it may be set to any other value at runtime. if ($this->rows_per_page > 0) { $this->lastpage = ceil($this->numrows/$this->rows_per_page); } else { $this->lastpage = 1; } // if 4. Ensure that $pageno is within range

This code is the same as before. 5. Construct LIMIT clause This code will construct the LIMIT clause for the sql SELECT statement. Note tha t if the value of $rows_per_page has been set to zero then this effectively turn s pagination off. if ($rows_per_page > 0) { $limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page; } else { $limit = ''; } // if 6. Issue the database query This code is the same as before. 7. Construct pagination hyperlinks This is a bit more complicated as it involves passing a series of parameters to an XSL stylesheet during the transformation process. Stage 1 is to extract certa in values from the database object and insert them into any array. $xsl_params['curpage'] = $dbobject->getPageNo(); $xsl_params['lastpage'] = $dbobject->getLastPage(); Stage 2 is to perform the XSL transformation using an XML file which was constru cted by the PHP script and a predefined XSL stylesheet. These processes are desc ribed in detail in the following documents: Using PHP 4's DOM XML functions to create XML files from SQL data. Using PHP 4's Sablotron extension to perform XSL Transformations. Stage 3 is performed during the XSL transformation. Although pagination is requi red in many scripts the code is absolutely identical, so being an efficient prog rammer I have defined it just once in its own template file. As this code has al ready been described in Generating dynamic web pages using XSL and XML I shall n ot bother to duplicate it here. This code can be included in any number of XSL stylesheets using the followng li ne of code: <xsl:include href="std.pagination.xsl"/> The predefined template can be executed when required using the following line o f code: <xsl:call-template name="pagination" /> That's all there is to it. Easy peasy, lemon squeezy. Sample software Sample software which shows pagination in action is available on my website, as described in article A Sample PHP Application. This software can be run online, or you can download all the source code and run it locally in your own PHP/MySQL environment.

Simple PHP/MySQL Pagination Code

Pagination Class The full PHP code for the the pagination class has been put into the PHP Snippet s section. You can find it here How to use $page = 1; // how many records per page $size = 10; // we get the current page from $_GET if (isset($_GET['page'])){ $page = (int) $_GET['page']; } // create the pagination class $pagination = new Pagination(); $pagination->setLink("list.php?page=%s"); $pagination->setPage($page); $pagination->setSize($size); $pagination->setTotalRecords($total_records); // now use this SQL statement to get records from your table $SQL = "SELECT * FROM mytable " . $pagination->getLimitSql(); To get the HTML code for the navigation list, you would simply call: $navigation = $pagination->create_links(); echo $navigation; // will draw our page navigation If you are familiar with CSS, you can easily style the way the page navigation b y wrapping the HTML the class produces with a DIV. If you want to see a working example of this pagination, then simply look at the GoodPHPTutorials page naviga tion links, since we use this code for the site. Previous 1 2 Setting the Variables As your database grows, showing all the results of a query on a single page is n o longer practical. This is where pagination comes in handy. You can display you r results over a number of pages, each linked to the next, to allow your users t o browse your content in bite sized pieces. <?php // Connects to your Database mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_erro r()); mysql_select_db("address") or die(mysql_error()); //This checks to see if there is a page number. If not, it will set it to page 1

if (!(isset($pagenum))) { $pagenum = 1; }

//Here we count the number of results //Edit $data to be your query $data = mysql_query("SELECT * FROM topsites") or die(mysql_error()); $rows = mysql_num_rows($data);

//This is the number of results displayed per page $page_rows = 4;

//This tells us the page number of our last page $last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pag es if ($pagenum < 1) { $pagenum = 1; } elseif ($pagenum > $last) { $pagenum = $last; }

//This sets the range to display in our query $max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows; This code is explained further on the next page.

Vous aimerez peut-être aussi