Wednesday, 18 February 2015

Pagination in PHP



<?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>";
    }
  
  
?>

No comments:

Post a Comment