Simple Pagination With PHP & MYSQL

Data Pagination is one of the comman task we have in web applications. When we get huge data form database insted of show all of it at once we will choose different ways to display that data. One such technique is php pagination.
In this tutorial we will use php and mysql to create simple pagination script.Pagination  lets you to paging your data in to number of pages. I simplifed code as much as possible. In the below example I have taken Country database with its country code. Please check on below link to check the demo. The countries database is included in countries.sql in the download file.
Simple Pagination With PHP & MYSQL
We will create these pages. -config.php (Database Configuration) -index.php -paginate.php Let's First create database table which we are going to use in our example. 

DataBase

  1. CREATE TABLE IF NOT EXISTS `countries` (
  2. `ccode` varchar(2) NOT NULL DEFAULT '',
  3. `country` varchar(200) NOT NULL DEFAULT '',
  4. PRIMARY KEY (`ccode`)
  5. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
The above is simple countries table. Just execute the above command in your MySQL command prompt orphpmyadmin area.

config.php


  1. $mysql_hostname = "host";
  2. $mysql_user = "usr";
  3. $mysql_password = "pass";
  4. $mysql_database = "db";
  5.  
  6. $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
  7. mysql_select_db($mysql_database, $bd) or die("Error on database connection");
This is the configuration file. Here you need to setup your database details. 

index.php

  1. <?php
  2. include('config.php'); //include of db config file
  3. include ('paginate.php'); //include of paginat page
  4. $per_page = 5; // number of results to show per page
  5. $result = mysql_query("SELECT * FROM countries");
  6. $total_results = mysql_num_rows($result);
  7. $total_pages = ceil($total_results / $per_page);//total pages we going to have
  8. //-------------if page is setcheck------------------//
  9. if (isset($_GET['page'])) {
  10. $show_page = $_GET['page']; //current page
  11. if ($show_page > 0 && $show_page <= $total_pages) {
  12. $start = ($show_page - 1) * $per_page;
  13. $end = $start + $per_page;
  14. } else {
  15. // error - show first set of results
  16. $start = 0;
  17. $end = $per_page;
  18. }
  19. } else {
  20. // if page isn't set, show first set of results
  21. $start = 0;
  22. $end = $per_page;
  23. }
  24. // display pagination
  25. $page = intval($_GET['page']);
  26. $tpages=$total_pages;
  27. if ($page <= 0)
  28. $page = 1;
  29.  
  30. ?>
Here we included two files config.php and paginate.php in first two lines. we can set $per_page to how many records you want to show on a page. We can calculate total pages depending on total results and per_page. Depending on the $_GET['page'] we can have start and end records. 

index.php(Displaying Data)

  1. <?php
  2. $reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
  3. echo '<div class="pagination"><ul>';
  4. if ($total_pages > 1) {
  5. echo paginate($reload, $show_page, $total_pages);
  6. }
  7. echo "</ul></div>";
  8. // display data in table
  9. echo "<table class='table table-bordered'>";
  10. echo "<thead><tr><th>country code</th> <th>Country Name</th></tr></thead>";
  11. // loop through results of database query, displaying them in the table
  12. for ($i = $start; $i < $end; $i++) {
  13. // make sure that PHP doesn't try to show results that don't exist
  14. if ($i == $total_results) {
  15. break;
  16. }
  17. // echo out the contents of each row into a table
  18. echo "<tr " . $cls . ">";
  19. echo '<td>' . mysql_result($result, $i, 'ccode') . '</td>';
  20. echo '<td>' . mysql_result($result, $i, 'country') . '</td>';
  21. echo "</tr>";
  22. }
  23. // close table>
  24. echo "</table>";
  25. // pagination
  26. ?>
This part will loop from $start to $end variable. The records will be displayed on the page. 

paginate.php

  1. function paginate($reload, $page, $tpages) {
  2. $adjacents = 2;
  3. $prevlabel = "&lsaquo; Prev";
  4. $nextlabel = "Next &rsaquo;";
  5. $out = "";
  6. // previous
  7. if ($page == 1) {
  8. $out.= "<span>".$prevlabel."</span>\n";
  9. } elseif ($page == 2) {
  10. $out.="<li><a href=\"".$reload."\">".$prevlabel."</a>\n</li>";
  11. } else {
  12. $out.="<li><a href=\"".$reload."&amp;page=".($page - 1)."\">".$prevlabel."</a>\n</li>";
  13. }
  14. $pmin=($page>$adjacents)?($page - $adjacents):1;
  15. $pmax=($page<($tpages - $adjacents))?($page + $adjacents):$tpages;
  16. for ($i = $pmin; $i <= $pmax; $i++) {
  17. if ($i == $page) {
  18. $out.= "<li class=\"active\"><a href=''>".$i."</a></li>\n";
  19. } elseif ($i == 1) {
  20. $out.= "<li><a href=\"".$reload."\">".$i."</a>\n</li>";
  21. } else {
  22. $out.= "<li><a href=\"".$reload. "&amp;page=".$i."\">".$i. "</a>\n</li>";
  23. }
  24. }
  25. if ($page<($tpages - $adjacents)) {
  26. $out.= "<a style='font-size:11px' href=\"" . $reload."&amp;page=".$tpages."\">" .$tpages."</a>\n";
  27. }
  28. // next
  29. if ($page < $tpages) {
  30. $out.= "<li><a href=\"".$reload."&amp;page=".($page + 1)."\">".$nextlabel."</a>\n</li>";
  31. } else {
  32. $out.= "<span style='font-size:11px'>".$nextlabel."</span>\n";
  33. }
  34. $out.= "";
  35. return $out;
  36. }
This is our main code which created the pagination html code and this code will be called from our index.php. In this first we need to set the $adjacents which is for how many li u want to display beside your current selected page. The remaining code will generate the li of the links.

Source: 
http://www.techumber.com/2012/08/simple-pagination-with-php-mysql.html

4 comments:

  1. I was recommended this web site by means of my cousin.
    I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!


    selenium training in Tambaram
    selenium training in Velachery
    selenium training in Omr
    selenium training in Annanagar
    selenium training in Chennai

    ReplyDelete
  2. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.
    python training in chennai
    Python Online training in usa
    python course institute in chennai

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete