Wednesday 30 January 2013

pagination


DataBase:

 <?php include("conn.php");
 ini_set ( 'max_execution_time', 1200);
 $tableName="messages";
 $targetpage = "index.php";
 $limit = 5;

 $query = "SELECT COUNT(*) as num  FROM $tableName";
 $total_pages = mysql_fetch_array(mysql_query($query));
 $total_pages = $total_pages[num];

 $stages = 3;
 $page = mysql_escape_string($_GET['page']);
 if($page){
  $start = ($page - 1) * $limit;
 }else{
  $start = 0;
  }

  // Get page data
 $query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
 $result = mysql_query($query1);

 // Initial page num setup
 if ($page == 0){$page = 1;}
 $prev = $page - 1;
 $next = $page + 1;  
 $lastpage = ceil($total_pages/$limit);
 $LastPagem1 = $lastpage - 1;
?>



CSS:

.paginate {
font-family:Arial, Helvetica, sans-serif;
 padding: 3px;
 margin: 3px;font-weight:bold;
}
.paginate a {
 padding:2px 5px 2px 5px;
 margin:2px;
 border:0 solid #999;
 text-decoration:none;  background-color:#666;
 color: #fff;font-weight:bold;
}
.paginate a:hover, .paginate a:active {
 border: 0 solid #999;
 background-color:#79FF79; font-weight:bold;
}
.paginate span.current {
    margin: 2px;
 padding: 2px 5px 2px 5px;
  border: 0 solid #999;

  font-weight: bold;
  background-color:#0F0;
  color: #FFF;
 }
 .paginate span.disabled {
  padding:2px 5px 2px 5px;
  margin:2px;
  background-color:#666;
  border:0 solid #eee;
  color:#DDD;font-weight:bold;
 }

index.php:


<table width="100%" border="0" cellspacing="0" cellpadding="0">
<!--Head start -->
  <tr><td style="height:110px; background-color:#FEFEFE;" align="center"> <img src="images/2.gif" width="656" height="105" align="middle" alt="" /></td> </tr>
<!--Head end -->
<!--menu start -->
  <?php /*?><tr> <td><?php include("menu/index3.php"); ?></td></tr>
<?php */?>  <!--menu end -->
  <tr><td>&nbsp;</td></tr>
  <!-- description -->
  <tr> <td><table width="80%" cellpadding="5" align="center"><tr><td align="center" class="th">Parts Catalogue View</td></tr></table></td></tr>
 <tr><td>&nbsp;</td></tr>
 <tr><td align="center">
    <?php
 $paginate = '';
 if($lastpage > 1)
 {
  $paginate .= "<div class='paginate'>";
  // Previous
  if ($page > 1){
   $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
  }else{
   $paginate.= "<span class='disabled'>previous</span>"; }


  // Pages
  if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
  {
   for ($counter = 1; $counter <= $lastpage; $counter++)
   {
    if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
    }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
   }
  }
  elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
  {
   // Beginning only hide later pages
   if($page < 1 + ($stages * 2))
   {
    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
    $paginate.= "...";
    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
   }
   // Middle hide some front and some back
   elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
   {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
    $paginate.= "...";
    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
   }
   // End only hide early pages
   else
   {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
   }
  }

    // Next
  if ($page < $counter - 1){
   $paginate.= "<a href='$targetpage?page=$next'>next</a>";
  }else{
   $paginate.= "<span class='disabled'>next</span>";
   }

  $paginate.= "</div>";


 }
 // pagination
 echo $paginate;?></td></tr>
 <tr><td style="background:none;">

 <BR />
 <table width="80%" border="0" cellspacing="2" class="tab1" cellpadding="5" align="center" style="text-align:center;">
  <tr class="tr"><td style="width:15px;">S.no</td><td style="width:50px;">Messages</td><td style="width:25px;">Edit</td><td style="width:25px;">Delete</td></tr>
       <?php
   
  $i=1;
    while($row1=mysql_fetch_array($result))
    {
     $messages=$row1['message'];
      $id=$row1['id'];
     ?>
      
     <tr class="tr1"><td><?php echo $i++; ?></td>
     <td><?php echo $messages; ?></td>
     <td><a href="edit_parts.php?edit=<?php echo $id; ?>"><img
      alt="edit" /></a></td>
     <td><a href="edit_parts.php?delete=<?php echo $id; ?>"> <img
     alt="delete"   onclick="return confirm('Really You want Delete this Parts');"  /></a></td></tr>
     <?php
    }
  
   
   
   ?>
 </table>
    </td></tr>
    <tr><td>&nbsp;</td></tr>
    <tr><td align="center">
    <?php
 $paginate = '';
 if($lastpage > 1)
 {

  $paginate .= "<div class='paginate'>";
  // Previous
  if ($page > 1){
   $paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
  }else{
   $paginate.= "<span class='disabled'>previous</span>"; }


  // Pages
  if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
  {
   for ($counter = 1; $counter <= $lastpage; $counter++)
   {
    if ($counter == $page){
     $paginate.= "<span class='current'>$counter</span>";
    }else{
     $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
   }
  }
  elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
  {
   // Beginning only hide later pages
   if($page < 1 + ($stages * 2))
   {
    for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
    $paginate.= "...";
    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
   }
   // Middle hide some front and some back
   elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
   {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
    $paginate.= "...";
    $paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
    $paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
   }
   // End only hide early pages
   else
   {
    $paginate.= "<a href='$targetpage?page=1'>1</a>";
    $paginate.= "<a href='$targetpage?page=2'>2</a>";
    $paginate.= "...";
    for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
    {
     if ($counter == $page){
      $paginate.= "<span class='current'>$counter</span>";
     }else{
      $paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
    }
   }
  }

    // Next
  if ($page < $counter - 1){
   $paginate.= "<a href='$targetpage?page=$next'>next</a>";
  }else{
   $paginate.= "<span class='disabled'>next</span>";
   }

  $paginate.= "</div>";


 }
 // pagination
 echo $paginate;?></td></tr>
    <tr><td colspan="8" align="middle" style="line-height:2em;"> <img src="images/Actions-edit-add-icon.png" width="32" height="32" align="top" alt="" />
            <a href="add_parts.php"><span style="color:#FF0; font-weight:bold;">Add Parts</span></a></td></tr>
 </table>




No comments:

Post a Comment