bottom
wait
Valid XHTML 1.0!
Get Firefox!
Programming tutorials - Pagination (PHP)
When in your data base you have a lot of records and you want to post them all, a bad idea will be to post them on only one page, this is why I suggest you to use pagination for this records, divide them in this way in many pages. A good example is http://www.google.com if you search something, it will return so many results so is impossible to post them on only one page, in this way they are divided in more pages, leting the possibility for the person who search to see the next or preview page or even to jump from the current page to 5 pages rearward or forward.
For the beginning we will connect to data base and will see how many records are in the table from which we want to extract the values. The value obtained will stock it as a value of variable $total_numbers and we will set how many records per page will be.
// INFORMATION ABOUT DATA BASE
$user = 'root';
$pass = '';
$server = 'localhost';
$data_base = 'test';
$table = '_table_';
$connect = mysql_connect($server, $user, $pass) or die (mysql_error());
$select_db = mysql_select_db($data_base, $connect) or die (mysql_error());
// HOW MANY RECORDSS ARE ON THE PAGE
$nr_per_page = 10;
// TOTAL NUMBER OF RECORDS
$sql_total = "SELECT count(*) AS nr FROM ".$table;
$query_total = mysql_query($sql_total, $connect);
$row = mysql_fetch_object($query_total);
$total_numbers = $row->nr;
Further will calculate how many pages we'll have dividing total numer of results from data base to number of records per page and takeing the next integer value using function ceil().
If you look carefully at pagination system on Google, you'll see that it is posting 10 results after and before current page if this thing is possible. The same thing we'll do us with $between variable, but we don't set it to 10 pages, but two, because I don't think that we'll have so many results as Google, anyhow the variable's value could be changed. We also calculate the value that we'll send it by GET for the last page, this we'll use it for the link "Last page".
// HOW MANY PAGES ARE
$pages = ceil($total_numbers/$nr_per_page);
// HOW MANY PAGES ARE BETWEEN CURRENT PAGE
$between = 2;
// LAST PAGE
$last = $pages*$nr_per_page-$nr_per_page;
In the moment when a link to another page (next, preview, first or last page) is clicked, we sent by GET method an http variable that we attribute to variable $limit_x. If we enter directly on the page, $_GET['limit_inf'] will not exist so $limit_x will be null that is not a good thing, in this way we attribute it the value 0. This variable we'll use it to select from data base the records that we'll post on the page starting with the record with number equal with the value of $limit_x and ending after $nr_per_page records found.
$limit_x = $_GET[limit_inf];
if($limit_x == '') $limit_x = 0;
$sql = "SELECT * FROM ".$table." LIMIT ".$limit_x.", ".$nr_per_page."";
$query = mysql_query($sql, $connect);
// SHOW THE RESULTS
while($row = mysql_fetch_array($query)){
echo $row[1].'<br />';
}
We calculate pages that will be posting as links. For this we need to know what page is the page where we are ($current_page) and what page is the first from which is starting the reckoning ($start) and the last page where the posting is stop ($stop).
$start represent the value that we'll post it for the beginning of pagination. If we have 100 pages for posting we do not post them all but only two ($between) before and after the current page. $start is precisely the beginning of pagination, that is if we are in the 10 th page, $start will be 8.
$stop is just like $start but reverse, this is posting the last value. In our exemple $stop will be 12, if this value exist, else it will be equal with the number of pages ($pages).
$current_page = $limit_x/$nr_per_page+1;
$start = 1;
$stop = $current_page+$between;
if ($current_page > $between) $start = $current_page-$between;
if ($pages < $stop) $stop = $pages;
All we have to do is to post the pagination for the results. If the number of results is smaller or equal than/with the variable set to be the maximum number of results per page, we don't post anything because it has no sense.
First time we post a link to the first page which it has an $_GET['limit_inf'] variable set to 0, then we start an for loop starting with the value of $start and stoping with the value of $stop, incrementing the value of $nr ($nr on start has a value equal with $start) on each loop. We want, also, that current page to be an non link. This thing we'll do if the value of http variable assigned to $limit_x is equal with $nr from precedent loop multiplyed with the number of posts per page ($nr_per_page).
For links we set a value for the variable sent by GET method, in accordance with is selecting the records from data base, equal with $nr's value from precedent loop multiply with the number of posts per page ($nr_per_page) and we'll show $nr as being the number of page from which we want to go. After for loop we post an link to last page, named "Last Page" that we'll set the http variable as being equal with the value of $last calculated above.
if($total_numbers > $nr_per_page){
// SHOW PAGINATION
echo "<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=0\" >First page</a>&nbsp;";
for($nr = $start; $nr <= $stop; $nr++ ){
if($limit_x != ($nr-1)*$nr_per_page)
echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=".(($nr-1)*$nr_per_page)."\" >".$nr."</a>&nbsp;";
else
echo "<span style=\"background-color:#999999; color: #ffffff;\">&nbsp;$nr&nbsp;</span>";
}
echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=".$last."\" >Last page</a>";
}
Here you have the entire source code for pagination. All you have to remember is that you need to set the values of variables for connecting to data base with yours infos and also to modify the value of $table, with the name of your mysql table.
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Pagination</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  6. </head>
  7. <body>
  8. <?
  9. // INFORMATION ABOUT DATA BASE
  10. $user = 'root';
  11. $pass = '';
  12. $server = 'localhost';
  13. $data_base = 'test';
  14. $table = '_table_';
  15. $connect = mysql_connect($server, $user, $pass) or die (mysql_error());
  16. $select_db = mysql_select_db($data_base, $connect) or die (mysql_error());
  17. // HOW MANY RECORDSS ARE ON THE PAGE
  18. $nr_per_page = 10;
  19. // TOTAL NUMBER OF RECORDS
  20. $sql_total = "SELECT count(*) AS nr FROM ".$table;
  21. $query_total = mysql_query($sql_total, $connect);
  22. $row = mysql_fetch_object($query_total);
  23. $total_numbers = $row->nr;
  24. // HOW MANY PAGES ARE
  25. $pages = ceil($total_numbers/$nr_per_page);
  26. // HOW MANY PAGES ARE BETWEEN CURRENT PAGE
  27. $between = 2;
  28. // LAST PAGE
  29. $last = $pages*$nr_per_page-$nr_per_page;
  30. $limit_x = $_GET[limit_inf];
  31. if($limit_x == '') $limit_x = 0;
  32. $sql = "SELECT * FROM ".$table." LIMIT ".$limit_x.", ".$nr_per_page."";
  33. $query = mysql_query($sql, $connect);
  34. // SHOW THE RESULTS
  35. while($row = mysql_fetch_array($query)){
  36. echo $row[1].'<br />';
  37. }
  38. $current_page = $limit_x/$nr_per_page+1;
  39. $start = 1;
  40. $stop = $current_page+$between;
  41. if ($current_page > $between) $start = $current_page-$between;
  42. if ($pages < $stop) $stop = $pages;
  43. if($total_numbers > $nr_per_page){
  44. // SHOW PAGINATION
  45. echo "<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=0\" >First page</a>&nbsp;";
  46. for($nr = $start; $nr <= $stop; $nr++ ){
  47. if($limit_x != ($nr-1)*$nr_per_page)
  48. echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=".(($nr-1)*$nr_per_page)."\" >".$nr."</a>&nbsp;";
  49. else
  50. echo "<span style=\"background-color:#999999; color: #ffffff;\">&nbsp;$nr&nbsp;</span>";
  51. }
  52. echo "&nbsp;<a href=\"".$_SERVER['PHP_SELF']."?limit_inf=".$last."\" >Last page</a>";
  53. }
  54. ?>
  55. </body>
  56. </html>
Print this page
bottom