Wednesday, February 28, 2018

simple paging tutorial in php using mysql

you create simple paging  in php easily.This is the complet source code for simple paging pgp in php.

<?php
$vendor_count_data="SELECT COUNT(id) FROM tableName";
$vendor_data="SELECT * FROM tableName";

$pnn = "?pn=";
$sql = $vendor_count_data;
$query = mysql_query($sql);
$row = mysql_fetch_row($query);
$rows = $row[0]; // This is the number of results we want displayed per page
$page_rows = 2; // This tells us the page number of our last page
$last = ceil($rows/$page_rows); // This makes sure $last cannot be less than 1
if($last < 1){ $last = 1; } // Establish the $pagenum variable
$pagenum = 1; // Get pagenum from URL vars if it is present, else it is = 1
if(isset($_GET['pn'])){ $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']); }
// This makes sure the page number isn't below 1, or more than our $last page
if ($pagenum < 1) { $pagenum = 1; } else if ($pagenum > $last) { $pagenum = $last; }
// This sets the range of rows to query for the chosen $pagenum
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is your query again, it is for grabbing just one page worth of rows by applying $limit
//$sql = "SELECT * FROM location_city WHERE status ='1' ORDER BY id DESC $limit";
$sql =$vendor_data." ".$limit;
$query = mysql_query($sql);
// This shows the user what page they are on, and the total number of pages
$textline1 = "$rows";
/* Showing 1 to 10 of 11 entries CUSTOM CODE */
//$textline2 = ($pagenum*10)." to ". $last;

if($pagenum==1)
{
$row_start=$pagenum;
}
else
{
$row_start=($page_rows*($pagenum-1))+1;
}
if(($pagenum*$page_rows)<$rows) {
$textline2 = $row_start." to ". ($pagenum*$page_rows);
}else
{
$textline2 = $row_start." to ". $rows;
}

$paginationCtrls = '';
if($last != 1){
$previous = $pagenum - 1;
if($pagenum!=1)
 {
 $paginationCtrls .= "<li><a href='".$pnn."1'>First</a></li><li><a href='".$pnn.$previous."'>Previous</a></li>";
 }
 
for($i = $pagenum-4; $i < $pagenum; $i++){ if($i > 0){ $paginationCtrls .= "<li><a href='".$pnn.$i."'>".$i."</a></li>"; /*}*/ } }
$paginationCtrls .= '<li><a href="javascript:void(0)" class="active">'.$pagenum.'</a></li>';
for($i = $pagenum+1; $i <= $last; $i++){ $paginationCtrls .= "<li><a href='".$pnn.$i."'>".$i."</a></li>";
if($i >= $pagenum+4){ break; } }
$next = $pagenum + 1;
if($last!=$pagenum)
{
$paginationCtrls .= "<li><a href='".$pnn.$next."'>Next</a></li> <li><a href='".$pnn.$last."'>Last</a></li>";
}

 } $list = '';

$i=$row_start;

while($row = mysql_fetch_array($query, MYSQL_ASSOC))
{ ?>
<p><?php echo $i; ?></p>
<p><?php echo $row['id']; ?> </p>

<?php $i++; } ?>
<?php //----show paging here------- ?>
<div class="pages-list">
<ul>
<?php echo $paginationCtrls; ?> 
</ul>
</div>
</div>

No comments:

Post a Comment