Pagination in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply

How this code is working ?

Good
1
20%
Bad
4
80%
 
Total votes: 5

rizvan.vadia
Forum Newbie
Posts: 1
Joined: Mon Jul 24, 2006 3:51 am

Pagination in PHP

Post by rizvan.vadia »

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 variables

Code: 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; ?>&nbsp;&nbsp;<?php echo $from + 1;?>&nbsp;-&nbsp;<?php if(($from + $limit)>$total) echo $total; else echo $from +$limit; ?>&nbsp;of&nbsp;<?php echo $total;?>&nbsp;&nbsp;|&nbsp;
<?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 "<< <"."&nbsp;";
			}
			else
			{
?>
				<a href="JavaScript:paging(1)" title="First"><?php echo "<<"; ?>&nbsp;</a>
				<a href="JavaScript:paging(<?php echo $page-1; ?>)" title="Previous"><?php echo "<"; ?>&nbsp;</a>
<?php
			}
			for($iCount = $stPage;$iCount<=$lastPage;$iCount++)
			{
				if($page == $iCount)
				{
					echo $iCount."&nbsp";
				}
				else
				{
?>
					<a href="JavaScript:paging(<?php echo $iCount; ?>)" ><?php echo $iCount; ?>&nbsp;</a>
<?php
				}
			}
			if($page == $pages)
			{
				echo "> >>";
			}
			else
			{
?>
				<a href="JavaScript:paging(<?php echo $page + 1; ?>)"  title="Next">>&nbsp;</a>
				<a href="JavaScript:paging(<?php echo $pages; ?>)"  title="Last">>></a>
<?php
			}
		}
		else
		{
			for($iCount = 1;$iCount <=$pages;$iCount++)
			{
				if($page == $iCount)
				{
					echo $iCount."&nbsp;";
				}
				else
				{
?>
					<a href="JavaScript:paging(<?php echo $iCount; ?>)" ><?php echo $iCount; ?>&nbsp;</a>
<?php
				}
			}
		}
?>
	</td>
</tr>
<?php
}
else
{
?>
	<tr class="<?php echo $pageClass; ?>">
		<td colspan="<?php echo $cols; ?>" class="<?php echo $pageClass; ?>">
			<?php echo $pageText; ?>&nbsp;&nbsp;<?php echo $from + 1;?>&nbsp;-&nbsp;<?php echo $total; ?>&nbsp;of&nbsp;<?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]
Last edited by rizvan.vadia on Mon Jul 24, 2006 5:28 am, edited 1 time in total.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You have got some nice little parse errors in the first block of code...so i voted Bad :P
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Post by MarK (CZ) »

I wouldn't count on JavaScript for such important element of the page as paging...
Post Reply