PHP pagination example with Mysql

PHP pagination example with Mysql

PHP pagination example with Mysql :-

This is Basic PHP pagination example with Mysql 

Make Databse with name : phpcooker_script

and export this table

 


CREATE TABLE IF NOT EXISTS `test` (
  `test_id` int(10) NOT NULL AUTO_INCREMENT,
  `test_title` varchar(100) NOT NULL,
  `test_description` text NOT NULL,
  `test_status` enum('0','1') NOT NULL COMMENT '0-active,1-inactive',
  PRIMARY KEY (`test_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;


--
-- Dumping data for table `test`
--

INSERT INTO `test` (`test_id`, `test_title`, `test_description`, `test_status`) VALUES
(1, 'test', 'test descriptrion', '1'),
(2, 'test2', 'test descriptrion2', '1'),
(3, 'test3', 'test descriptrion3', '1'),
(4, 'test4', 'test descriptrion4', '1');

Make :-  pagination.php


<?php

class pagination{
    
    
    private $host     = "localhost";
    private $user     = "root";
    private $password = "";
    private $database = "phpcooker_script";
    private $conn     = "";
    function __construct() {
        $this->conn = @mysql_connect($this->host,$this->user,$this->password);
        mysql_select_db($this->database,$this->conn);
    }
        

    
var $offset =0;
var $page=0 ;
var $lpp = 0;
var $total_data = 0;
var $cn="";
var $php_self="";
var $lpd = 1;


    


function pagination($table,$dpp=1,$lpp=5,$cn="",$field="*")
{
    if(isset($_REQUEST['lpd']))
    {
    $this->lpd = $_REQUEST['lpd'];
    }

$this->php_self = htmlspecialchars($_SERVER['PHP_SELF']);
$this->cn = $cn;
$this->lpp = $lpp;
$this->total_data = count($this->select($table,$cn,$field));
$this->page = ceil($this->total_data / $dpp );
$this->offset = $dpp * ($this->lpd - 1);
if($this->lpp > $this->page )
{
$this->lpp = $this->page;
}
$cn  .=" limit $this->offset,$dpp";

$result = $this->select($table,$cn,$field);

return $result;


}

    function nav(){
    
    $this->lpp;
    $this->total_data;
    $this->cn;
    $links = "";
    $batch = ceil($this->lpd / $this->lpp );
    $end = $batch * $this->lpp;
    if ($end > $this->page) {
            $end = $this->page;
        }
    $start = $end - $this->lpp + 1;

    for($i = $start; $i <= $end; $i ++) {
            if ($i ==  $this->lpd) {
                echo '<div class="pzn1" > <a  class="pzz1" >'. $i . '</a></div>';
            } else {
                echo'<div class="pzn" > <a href='.$this->php_self.'?lpd='.$i.' class="pzz" >'. $i . '</a></div>';
    
            }
        }

    }

      function nexts($tag = 'next') {
    $next = $this->lpd + 1;
        if ($this->total_data == 0)
            return FALSE;
        
        if ($this->lpd < $this->page) {
            
            return'<div class="pzn" > <a href='.$this->php_self.'?lpd='.$next.' class="pzz" >'. $tag . '</a></div>';
            
            
        } else {
            return '<div class="pzn1" ><a class="pzz1" >' . $tag . '</a></div>';
        }
    }
    
    
    
          function priv($tag = 'priv') {
        $priv = $this->lpd - 1;
        if ($this->total_data == 0)
            return FALSE;
        
        if ($this->lpd >1) {
            
            return'<div class="pzn" > <a href='.$this->php_self.'?lpd='.$priv.' class="pzz" >'. $tag . '</a></div>';
            
                
            
        } else {
            return '<div class="pzn1" ><a class="pzz1" >' . $tag . '</a></div>';
        }
    }


    function select($table,$condition="",$field="*"){
      $sql="select $field from $table $condition";
      $aryResult=array();
    
        $result=mysql_query($sql);

        if(!is_null($result))
        {
            while($row=mysql_fetch_array($result)) { $aryResult[]=$row; }
            return $aryResult;
        }
        else
        {
            return NULL;
        }
    }    
    
    
    
    

}

?>

 

make : - example.php


<!DOCTYPE html>
<html lang="en">
<head>
  <title>pagination Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link  href="pagination.css" rel="stylesheet">
</head>
<body>

<div class="container">
  <h2>pagination Example</h2>
           
  <table class="table">
    <thead>
      <tr>
      <th>title</th>
    <th>description</th>
      </tr>
    </thead>
    <tbody>
    <?php
require_once ('pagination.php');

$db = new pagination();

$datas = $db->pagination('test',3,5);
foreach($datas as $data)
{
    ?>
      <tr>
      <td><?php echo $data["test_title"]; ?></td>
    <td><?php echo $data["test_description"]; ?></td>
      </tr>
      <?php     
}
    

?>
    </tbody>
  </table>
  <div style="text-align:center;">

<?php

echo $db->priv();
    echo $db->nav();
    
    //Display the link to next page: >>
    echo $db->nexts();

?>
</div>
 
</div>

 


</body>
</html>

 

make : - pagination.css

 


.pzn{
float:left;
height:auto;
color:#000;
width: 40px;
border: 1px solid navy;
margin: 2px;
padding:2px;
text-align:center;
background-color:#000;
 }
 .pzz{
text-decoration:none;     
border: none;
background-color:#000;
color:#fff;
width: 40px;
height:30px;
 }
 
 .pzn1{
float:left;
height:auto;
width: 40px;
border: 1px solid navy;
margin: 2px;
padding:2px;
text-align:center;
background-color:#fafafa;
 }
 .pzz1{
text-decoration:none;     
border: none;
background-color:#fafafa;
color:#000;
width: 40px;
height:30px;
 }
 

Download Here

Total Coments (0)