Site Links
Blogs
Affiliates
In this tutorial I will show you how to take a list of states from a database and break up the states into groups of 10 per page.
First we need to create a database that we can extract our data from. I put up a short script you can copy from my google docs, paste it into a php file and run it to create the database table and fields.
For a copy of the database script Click Here...
Since we are collecting our data from the database we need to open a connection.
<?php // OPEN DATABASE ?>
In this script we are going to determine how much data we want to display per page.
<?php // ROWS DISPLAYED PER PAGE $rows_per_page = 10; // GET PAGE NUMBER $page = $_GET['page']; // URL CLEAN UP $self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; // GET LIST OF STATES $offset = ($page) ? ($page - 1) * $rows_per_page : 0; $query = "SELECT * ". "FROM states_list ". "ORDER BY id ". "LIMIT {$offset},{$rows_per_page}"; // GET NUMBER OF PAGES ?> <div style="width: 500px; margin:auto; border:1px #666666 solid;"> <div style="background:#CCCCCC; text-align:center; padding:4px; border-bottom:1px #666666 solid;"><strong>LIST OF STATES</strong></div> <div style="text-align:center; padding:4px; border-bottom:1px #666666 solid;"><?=$row['states']?></div> <?php } ?> <span style="float:right"> <?=$prev = ($NumPgs > 0 && $page!=1) ? "<a href=\"{$self}page=".($page-1)."\"><<Prev</a> ": "<<Prev "; ?> [Page <?php echo $page; ?>] <?=$next = ($page < $NumPgs) ? "<a href=\"{$self}page=".($page+1)."\"> Next>></a>": " Next>>";?> </span> <b> <?=$offset+1?> to <?=$offset+$rows_per_page?>, of <?=$listings_total?> States</b> </div> ?>
$rows_per_page = 10;
On line 3 of our pagination script we want to set the number of data we want to display per page. I put this at the top so it's easy to find for later pagination template use.
$page = $_GET['page']; $offset = (!empty($page)) ? $page : $page = 1;
Since we are using URL parameters to get our page number, we need to to check the URL and see if our page param is empty or not. So, for example, if the URL to our pagination page is http://www.hollen-b.com/tutorial/pagination_demo.php we will default to page 1. If the URL to our pagination page is http://www.hollen-b.com/tutorial/pagination_demo.php?page=2 we go to page 2.
$self = $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; $self = str_replace("page={$offset}", "", $self);
Without this every time someone clicks a prev or next link our parameters will start to stack up in our URL. So we need to get the URL and remove the old page parameter.
$offset = ($page) ? ($page - 1) * $rows_per_page : 0; $query = "SELECT * ". "FROM states_list ". "ORDER BY id ". "LIMIT {$offset},{$rows_per_page}"; $result = mysql_query($query);
Every time we load the page we are going to run two arguments to MySQL using LIMIT X, Y. X is the starting point, and Y is the total amount of records we want to display. To get X in our case is $offset, we need to first get the page number were on. Subtract the page number by 1, multiply that number by the total rows per page and we now should have the X value we need. Y is simply our number of rows we want to display which was set on line 3.
$ltq = mysql_query("SELECT * FROM states_list"); $listings_total = mysql_num_rows($ltq); $NumPgs = ceil($listings_total / $rows_per_page);
If we query the database for our total number of rows, we can divide that by the total rows per page, which should give us our total number of pages. By using ceil function we can round our total number of pages to the next highest integer.