Last updated on July 31st, 2016 at 11:16 am

This tutorial is for beginners who want to make pagination with PHP and MYSQL. Pagination is one of the best way to display thousands of result into single page. Pagination with PHP lets you to paging your data into number of pages.

 

Now start coding: in this we need to create three pages

  • config.php
  • index.php
  • function.php

config.php

<?php

define("DBhost", "127.0.0.1"); // Host name

define("DBuser", "root"); // DB username

define("DBpwd", ""); // DB password

define("DBname", "pagination"); // DB name

$con = mysql_connect(DBhost, DBuser, DBpwd);

if(!$con){

               echo 'Error connecting with DataBase.'. mysql_error();

               exit();

}

mysql_select_db(DBname, $con);

?>

function.php

 

contain main pagination code.

<?php

function pagi($Rpp=null, $page = null, $url=null, $count=null){

                              $adjacents = "2";

                             

                              $page = ($page == 0 ? 1 : $page);

                              $start = ($page - 1) * $Rpp;

                             

                              $prev = $page - 1;

                              $next = $page + 1;

                              $lastpage = ceil($count/$Rpp);

                              $lpm1 = $lastpage - 1;

                             

                              $pagination = "";

                              if($lastpage > 1)

                              {

                              $pagination .= "<div class='pag-nation'>";

                              if ($lastpage < 7 + ($adjacents * 2))

                              {

                              for ($counter = 1; $counter <= $lastpage; $counter++)

                              {

                              if ($counter == $page)

                              $pagination.= "<span class='current'>$counter</span>";

                              else

                              $pagination.= "<a href='{$url}$counter'>$counter</a>";

                              }

                              }

                              elseif($lastpage > 5 + ($adjacents * 2))

                              {

                              if($page < 1 + ($adjacents * 2))

                              {

                              for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)

                              {

                              if ($counter == $page)

                              $pagination.= "<span class='current'>$counter</span>";

                              else

                              $pagination.= "<a href='{$url}$counter'>$counter</a>";

                              }

                              $pagination.= "<span>...</span>";

                              $pagination.= "<a href='{$url}$lpm1'>$lpm1</a>";

                              $pagination.= "<a href='{$url}$lastpage'>$lastpage</a>";

                              }

                              elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))

                              {

                              $pagination.= "<a href='{$url}1'>1</a>";

                              $pagination.= "<a href='{$url}2'>2</a>";

                              $pagination.= "<span>...</span>";

                              for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)

                              {

                              if ($counter == $page)

                              $pagination.= "<span class='current'>$counter</span>";

                              else

                              $pagination.= "<a href='{$url}$counter'>$counter</a>";

                              }

                              $pagination.= "<span>...</span>";

                              $pagination.= "<a href='{$url}$lpm1'>$lpm1</a>";

                              $pagination.= "<a href='{$url}$lastpage'>$lastpage</a>";

                              }

                              else

                              {

                              $pagination.= "<a href='{$url}1'>1</a>";

                              $pagination.= "<a href='{$url}2'>2</a>";

                              $pagination.= "<span>...</span>";

                              for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)

                              {

                              if ($counter == $page)

                              $pagination.= "<span class='current'>$counter</span>";

                              else

                              $pagination.= "<a href='{$url}$counter'>$counter</a>";

                              }

                              }

                              }

                             

                              if ($page < $counter - 1){

                              $pagination.= "<a href='{$url}$next'>></a>";

                              //$pagination.= "<a href='{$url}$lastpage'>Last</a>";

                              }else{

                              $pagination.= "<a href='{$url}1'>Back</a>";

                              //$pagination.= "<span class='current'>Next</span>";

                              //$pagination.= "<span class='current'>Last</span>";

                              }

                              $pagination.= "</div>";

                              $pagination .= "<div class='pag-cnt'>Page $page of $lastpage</div>\n";

                              }

                              return $pagination;

               }

?>

Index.php

Contain output of pagination results

<?php

include_once('config.php');

include_once('function.php');

/* Load pagination */

$page = 1;

$limit = 2; // Set Limit according to you 1 - 5 -10

$start = 0;

if(isset($_GET['page']) && $_GET['page'] !=""){

               $page = $_GET['page'];

               if(!is_numeric($page)){

                              $page = 1;

               }

}

$start = ($page - 1)*$limit;

$rows = mysql_num_rows(mysql_query("SELECT * FROM  `page_table` "));

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Pagination with php - simple and easy | trinity tuts</title>

<link rel="stylesheet" type="text/css" href="css.css" /></head>

 

<body>

    <div class="hdr">TrinityTuts</div>

    <h2 class="hlab">Simple Pagination with PHP and MYSQL!!</h2>

    <div class="cont">

<?php

               $qur =  mysql_query("SELECT * FROM  `page_table` order by id desc limit $start, $limit") or die(mysql_error());

               while($r = mysql_fetch_array($qur)){

                              extract($r);

?>

                              <div class="title"><?php echo $title;?></div>

        <div class="dt"><?php echo $dateTime;?></div>

        <div class="desc"><?php echo $description;?></div>

<?php }?>

               </div>

    <div class="pagination-nav">

        <?php echo pagi($limit, $page, "?page=", $rows);?>

        <div class="clear"></div>

    </div>

    <div class="resv">

        <a href="http://trinityblog.in">© TrinityBlog.in</a>

        <a href="http://shivnsaigroup.com" style="float:right">Powered By:- Shiv 'n' Sai Developer's</a>

    </div>

</body>

</html>

<?php mysql_close();?>

Simple just and easy Only need just basic understanding of php code ans mysql.

Posted in: php