Tuesday, 4 August 2015

Pagination in PHP

07:12

<?php

    mysql_connect("localhost","root","")or die("Problems...");
    mysql_select_db("payroll");

    $per_page = 2;
    $pages_query = mysql_query("SELECT COUNT('id') FROM  employee");
    echo $pages_query."<br>";
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    echo $pages;
   
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;
   
    $query = mysql_query("SELECT *  FROM employee LIMIT $start, $per_page");
   
    while($row = mysql_fetch_assoc($query))
   
   
    {
   
   

echo '<form action=update.php  method=post      enctype="multipart/form-data">
<table border=1  cellspacing=3 cellpadding=2 bgcolor=orange  bordercolor=black>
<tr><td>DATE OF SIGN UP</td><td><INPUT TYPE=TEXT NAME="name"  value="'.$row['name'].'"></td></tr>
<tr><td>NAME</TD><td><INPUT TYPE=TEXT NAME="phone"      value="'.$row['phone'].'"></td></tr>
<tr><td>ADDRESS</TD><TD><input type=text name="salary"        value="'.$row['salary'].'"></td></tr>

<tr><td> </td><td>
<input type=hidden name=hidden  value="'.$row['emp_id'].'">
<input type=submit name=update value=update></td></tr>


</TABLE>


</FORM>';


   
   
   
    }
   
    $prev = $page - 1;
    $next = $page + 1;
   
    if(!($page<=1)){
        echo "<a href='pagination1.php?page=$prev'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages){
   
        for($x=1;$x<=$pages;$x++){
            echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
       
        }
   
    }
   
    if(!($page>=$pages)){
        echo "<a href='pagination1.php?page=$next'>Next</a>";
    }
   
   
?>


=============================================================================

PHP EXAMPLE  with mysqli  AND PAGINATION :
=============================================================================

<?php

    $con=mysqli_connect("localhost","root","","payroll");
    
    $per_page = 2; //per page record



    
$pages_query = mysqli_query($con, 'SELECT COUNT(id) FROM employee'); 

$row = mysqli_fetch_array($pages_query);

$pages = ceil($row[0] / $per_page);


    

//echo $pages;
   
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1; //page number throung url 

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

echo $sql="SELECT *  FROM employee LIMIT $start, $per_page";
   
  $query = mysqli_query($con,$sql);
 
while($row=mysqli_fetch_array($query))
{
 
echo "id is" .$row['id'];
echo $row['name'];
echo $row['phone'];
echo $row['salary'];

echo '<br>';  
}
 
$prev = $page - 1;
    $next = $page + 1;
   
    if(!($page<=1))
{
        echo "<a href='pag.php?page=$prev'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages){
   
        for($x=1;$x<=$pages;$x++){
            echo ($x == $page) ? '<strong><a href="?page='.$x.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'">'.$x.'</a> ';
       
        }
   
    }
   
    if(!($page>=$pages))
{
        echo "<a href='pag.php?page=$next'>Next</a>";
    }
   
   
?>

=============================================================================

PHP EXAMPLE WITH  SEARCH AND PAGINATION :
=============================================================================
(1)create table details : 
SQL>create table  details
(id  int primary key auto_increment,
 name   varchar(200),
city     varchar(200)
);

(2)write code for search.html file:

<form action="search.php" method="get">
name <input type=text name=search>
<input type=submit name=submit value=submit>

</form>

(3)write code for search.php file:

<?php
    $search=$_GET['search'];
    mysql_connect("localhost","root","")or die("Problems...");
    mysql_select_db("ajax");

    $per_page = 2;
    $pages_query = mysql_query("SELECT COUNT('id') FROM  details where name='$search' ");
    echo $pages_query."<br>";
    $pages = ceil(mysql_result($pages_query, 0) / $per_page);
    echo $pages;
   
    $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start = ($page - 1) * $per_page;
   
    $query = mysql_query("SELECT *  FROM details where name='$search' LIMIT $start, $per_page");
   
    while($row = mysql_fetch_assoc($query))
   
   
    {
   
   echo $row['id'];

  echo $row['name'];
echo $row['city'];


   
   
   
    }
   
    $prev = $page - 1;
    $next = $page + 1;
   
    if(!($page<=1)){
        echo "<a href='search1.php?page=$prev&search=$search'>Prev</a> ";
    }

    if($pages>=1 && $page<=$pages){
   
        for($x=1;$x<=$pages;$x++){
            echo ($x == $page) ? '<strong><a href="?page='.$x.'&search='.$search.'">'.$x.'</a></strong> ' : '<a href="?page='.$x.'&search='.$search.'">'.$x.'</a> ';
       
        }
   
    }
   
    if(!($page>=$pages)){
        echo "<a href='search1.php?page=$next&search=$search'>Next</a>";
    }
   
   

?>








=================================================================================
PHP EXAMPLE WITH  SEARCH AND PAGINATION  USING OBJECT ORIENTED CONCEPT:
=================================================================================
First create  table  toy:

SQL>
 CREATE TABLE  toy (
  id int(8) NOT NULL AUTO_INCREMENT,
  name varchar(255) NOT NULL,
  code varchar(20) NOT NULL,
  category varchar(255) NOT NULL,
  price double NOT NULL,
  stock_count bigint(16) NOT NULL,
  PRIMARY KEY (id)




(1)write code for dbcontroller.php file:
<?php
class DBController {
private $host = "localhost";
private $user = "root";
private $password = "";
private $database = "ajax";

function __construct() {
$conn = $this->connectDB();
if(!empty($conn)) {
$this->selectDB($conn);
}
}

function connectDB() {
$conn = mysql_connect($this->host,$this->user,$this->password);
return $conn;
}

function selectDB($conn) {
mysql_select_db($this->database,$conn);
}

function runQuery($query)
{
$result = mysql_query($query);
while($row=mysql_fetch_assoc($result)) {
$resultset[] = $row;
}
if(!empty($resultset))
return $resultset;
}

function numRows($query) {
$result  = mysql_query($query);
$rowcount = mysql_num_rows($result);
return $rowcount;
}
}
?>
(2)write code for  perpage.php  file:

<?php
function perpage($count, $per_page = '10',$href) {
$output = '';
$paging_id = "link_perpage_box";
if(!isset($_POST["page"])) $_POST["page"] = 1;
if($per_page != 0)
$pages  = ceil($count/$per_page);
if($pages>1) {

if(($_POST["page"]-3)>0) {
if($_POST["page"] == 1)
$output = $output . '<span id=1 class="current-page">1</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="1" />';
}
if(($_POST["page"]-3)>1) {
$output = $output . '...';
}

for($i=($_POST["page"]-2); $i<=($_POST["page"]+2); $i++) {
if($i<1) continue;
if($i>$pages) break;
if($_POST["page"] == $i)
$output = $output . '<span id='.$i.' class="current-page" >'.$i.'</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $i . '" />';
}

if(($pages-($_POST["page"]+2))>1) {
$output = $output . '...';
}
if(($pages-($_POST["page"]+2))>0) {
if($_POST["page"] == $pages)
$output = $output . '<span id=' . ($pages) .' class="current-page">' . ($pages) .'</span>';
else
$output = $output . '<input type="submit" name="page" class="perpage-link" value="' . $pages . '" />';
}

}
return $output;
}

function showperpage($sql, $per_page = 10, $href) {
$result  = mysql_query($sql);
$count   = mysql_num_rows($result);
$perpage = perpage($count, $per_page,$href);
return $perpage;
}
?>
(3)write code for  index.php  file  : your main file:
<?php
require_once("perpage.php");
require_once("dbcontroller.php");
$db_handle = new DBController();

$name = "";


$queryCondition = "";
if(!empty($_POST["search"])) {
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {

$queryCases = array("name","code");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "name":
$name = $v;
$queryCondition .= "name LIKE '" . $v . "%'";
break;

}
}
}
}
$orderby = " ORDER BY id desc";
$sql = "SELECT * FROM toy " . $queryCondition;
$href = 'index.php';

$perPage = 2; 
$page = 1;
if(isset($_POST['page'])){
$page = $_POST['page'];
}
$start = ($page-1)*$perPage;
if($start < 0) $start = 0;

$query =  $sql . $orderby .  " limit " . $start . "," . $perPage;
$result = $db_handle->runQuery($query);

if(!empty($result)) {
$result["perpage"] = showperpage($sql, $perPage, $href);
}
?>
<html>
<head>
<title>CODE BY OM SIR FOR with Search and Pagination</title>
<link href="style.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2>CODE BY OM SIR FOR with Search and Pagination</h2>
<div style="text-align:right;margin:20px 0px 10px;">
<a id="btnAddAction" href="add.php">Add New</a>
</div>
    <div id="toys-grid">  
<form name="frmSearch" method="post" action="index.php">
<div class="search-box">
<p><input type="text" placeholder="Name" name="search[name]" class="demoInputBox" value="<?php echo $name; ?>" />
<input type="submit" name="go" class="btnSearch" value="Search"><input type="reset" class="btnSearch" value="Reset" onclick="window.location='index.php'"></p>
</div>

<table cellpadding="10" cellspacing="1">
        <thead>
<tr>
          <th><strong>Name</strong></th>
          <th><strong>Code</strong></th>      
          <th><strong>Category</strong></th>
<th><strong>Price</strong></th>
<th><strong>Stock Count</strong></th>
<th><strong>Action</strong></th>

</tr>
</thead>
<tbody>
<?php
foreach($result as $k=>$v) {
if(is_numeric($k))
{
?>
          <tr>
<td><?php echo $result[$k]["name"]; ?></td>
          <td><?php echo $result[$k]["code"]; ?></td>
<td><?php echo $result[$k]["category"]; ?></td>
<td><?php echo $result[$k]["price"]; ?></td>
<td><?php echo $result[$k]["stock_count"]; ?></td>
<td>
<a class="btnEditAction" href="edit.php?id=<?php echo $result[$k]["id"]; ?>">Edit</a> <a class="btnDeleteAction" href="delete.php?action=delete&id=<?php echo $result[$k]["id"]; ?>">Delete</a>
</td>
</tr>
<?php
}
}
if(isset($result["perpage"])) {
?>
<tr>
<td colspan="6" align=right> <?php echo $result["perpage"]; ?></td>
</tr>
<?php } ?>
<tbody>
</table>
</form>
</div>
</body>
</html>

Output:



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

1 comments:

  1. Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post. Interesting post! Thanks for writing it.
    we also provide php training in delhi

    ReplyDelete

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top