Pagination in PHP
Posted: Mon Jul 24, 2006 4:15 am
Pimptastic | Please use
After initializing these variables u need to put the following code in file and call it as compaging.php
and include this file in any of the table row of your code.
And at last u need to define JS fucntion
Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
For pagination u need to initialize some variablesCode: Select all
<?php
$page; // Page number from url initialy page should be set to 1
$limit; //Number of records perpage
$from = limit*($page-1);
$disPage; //Number of pages to be displayed +/- 10/2
$pageText; //Text to be displyed in front of Paging
$pageClass; //style sheet for the paging row
$cols; //collspan in the table
$from; //offset
$total; // Total number of record comming from database
?>After initializing these variables u need to put the following code in file and call it as compaging.php
and include this file in any of the table row of your code.
Code: Select all
<?php
if($total > $limit)
{
?>
<tr class="<?php echo $pageClass; ?>">
<td colspan="<?php echo $cols; ?>" class="<?php echo $pageClass; ?>">
<?php echo $pageText; ?> <?php echo $from + 1;?> - <?php if(($from + $limit)>$total) echo $total; else echo $from +$limit; ?> of <?php echo $total;?> |
<?php
$pages = intval($total / $limit,10);
if(($total % $limit)!=0)
$pages++;
if($pages > $disPage/2)
{
if(($page-5)<1)
$stPage = 1;
else
$stPage = $page-5;
if(($page+5)>=$pages)
$lastPage = $pages;
else
$lastPage = $page+5;
if($page == 1)
{
echo "<< <"." ";
}
else
{
?>
<a href="JavaScript:paging(1)" title="First"><?php echo "<<"; ?> </a>
<a href="JavaScript:paging(<?php echo $page-1; ?>)" title="Previous"><?php echo "<"; ?> </a>
<?php
}
for($iCount = $stPage;$iCount<=$lastPage;$iCount++)
{
if($page == $iCount)
{
echo $iCount." ";
}
else
{
?>
<a href="JavaScript:paging(<?php echo $iCount; ?>)" ><?php echo $iCount; ?> </a>
<?php
}
}
if($page == $pages)
{
echo "> >>";
}
else
{
?>
<a href="JavaScript:paging(<?php echo $page + 1; ?>)" title="Next">> </a>
<a href="JavaScript:paging(<?php echo $pages; ?>)" title="Last">>></a>
<?php
}
}
else
{
for($iCount = 1;$iCount <=$pages;$iCount++)
{
if($page == $iCount)
{
echo $iCount." ";
}
else
{
?>
<a href="JavaScript:paging(<?php echo $iCount; ?>)" ><?php echo $iCount; ?> </a>
<?php
}
}
}
?>
</td>
</tr>
<?php
}
else
{
?>
<tr class="<?php echo $pageClass; ?>">
<td colspan="<?php echo $cols; ?>" class="<?php echo $pageClass; ?>">
<?php echo $pageText; ?> <?php echo $from + 1;?> - <?php echo $total; ?> of <?php echo $total;?>
</td>
</tr>
<?php
}
?>And at last u need to define JS fucntion
Code: Select all
function paging(page)
{
document.location = "YourURL?page="+page;
}Pimptastic | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]