Record pagination question

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
Mechaworx
Forum Newbie
Posts: 8
Joined: Sat May 22, 2010 1:26 pm

Record pagination question

Post by Mechaworx »

Hi have come up with a record pagination system but unfortunately will soon reach the limits of its capabilities.

Currently the system is set up in the following format:

[PREV] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [NEXT]

And here is the php code that generates this output:

Code: Select all

<?php  

mysql_connect("localhost", "root", "");
mysql_select_db("masscic");

//determine the number of records to be displayed per page
$per_page = 5;

//variable in order to declare a starting point 
$records = (isset($_GET['records']) ? $_GET['records'] : NULL); 

//Count the records in the database
$record_count = mysql_num_rows(mysql_query("SELECT * FROM articles"));

//if the mysql_paginationtest2.php page is found through a search engine set the starting point as below
if (!$records) {
	$records = 0;
}

//set up articles page navigation
	$prev = $records - $per_page;
	$next = $records + $per_page;

if (!($records <=0)) { //show previous button but shows nothing if on the first page
	//declare variable and echo below to html page
		echo '<li><a href="mysql_paginationtest2.php?records='.$prev.'">Prev</a></li>';
 	}
	//set variable for first page and show page numbers in a for loop  
	$i = 1;
	for ($x=0; $x < $record_count; $x=$x + $per_page)
	{
	//set current page activation to show user current page status
		if ($records!=$x)
			echo '<li><a href="mysql_paginationtest2.php?records='.$x.'">'.$i.'</a></li>';
		else
			echo '<li><a href="mysql_paginationtest2.php?records='.$x.'"class="active">'.$i.'</a></li>';
		$i++;
	}
	
	if (!($records >=$record_count - $per_page)) {//show next button but show nothing if on the last page
	//declare variable and echo below to html page
		echo '<li><a href="mysql_paginationtest2.php?records='.$next.'"> Next</a></li>';
	}
?>

Unfortunately due to html design space constraints, my page navigation list will soon drop to another line. Which is something I would like to avoid. So I am trying to set up a page navigation similar to this boards php topics navigation like so:

[PREV] [1] [2] [3] [4] [5] ... [200] [NEXT]

Is there anything I can add to the above code to achieve this?

Thanks for any help.
Gerry
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Record pagination question

Post by Jonah Bron »

Your code needs to display a max of 5 links on each side of the current page. Any more than that, and it must show ... and a link to the first/last, depending on which side it's on. And if the other side has less than it's quota (5), than show the difference in links on the other side. Here's some pseudo code.

[syntax]
left_quota = current_page
if left_quota > 6
left_quota = 6
right_quota = 6
else
right_quota = 12 - current_page
end
if total_pages - current_page < 6
left_quota += total_pages - current_pages
right_quota = total_pages - current_pages
end
[/syntax]
Got a brain cramp now. That should get the number of links before and after the current one. Set your code up so that if the number is 6, display 5 and the ... and then the beginning/end.
Mechaworx
Forum Newbie
Posts: 8
Joined: Sat May 22, 2010 1:26 pm

Re: Record pagination question

Post by Mechaworx »

Hey Thanks Jonah.

I'll need a bit to study that and see if I can get what you're doing.

Thanks
Gerry
Post Reply