Pagination - back button problems?

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
dunc
Forum Newbie
Posts: 13
Joined: Mon Sep 04, 2006 10:17 am

Pagination - back button problems?

Post by dunc »

Morning, ladies and gents.

I'm using a pagination function on my website to give page numbers for search results (it's a fishkeeping website with numerous profiles so a bit of a necessity).

The basics are that $act, $page and $limit variables are sent through URLs and any additional search preferences (region, category or whatever) are stored in session variables if it's a fresh search (if $f==fresh, <input type=hidden name=f> on the search form), or retrieved from session variables if it isn't.

When the results are displayed, as expected the results link to either a separate fish profile page or in the case of our community database it'll link to community.php?id=int and display the individual details of a club or affiliate.

My problem is that after viewing a club or fish profile, if the user hits back, I get the classic "page cannot be displayed".

Most of the code is here:

Code: Select all

if ($num_rows > 0)
{
	echo '<table class="kbTable" align="left" width="604">
	<p><h2>'.$title.'s</h2></p>';
	
	//-----> Start page maths
	$pages = intval($num_rows/$limit); // Number of results pages.

	// $pages now contains int of pages, unless there is a remainder from division.

	if ($num_rows % $limit)
		$pages++;// has remainder so add one page

	$current = ($page/$limit) + 1; // Current page number.

	if (($pages < 1) || ($pages == 0))
		$total = 1;// If $pages is less than one or equal to 0, total pages is 1.
	else
		$total = $pages;// Else total pages is $pages value.

	$first = $page + 1; // The first result.

	if (!((($page + $limit) / $limit) >= $pages) && $pages != 1)
		$last = $page + $limit;//If not last results page, last result equals $page plus $limit.

	else
		$last = $num_rows;// If last results page, last result equals total number of results.
	//-----> Finish page maths

	echo '<tr>
	<td align="left" class="bluTxt10px" colspan="2">
	Page <b>'.$current.'</b> of <b>'.$total.'</b>. Results <b>'.$first.'</b> - <b>'.$last.'</b> of <b>'.$num_rows.'</b>
	</td>
	<td align="right" class="bluTxt10px">Results per page: ';
	if ($limit == 6) { echo '6 |'; }
	else { echo '<a href="'.$PHP_SELF.'?act=$act&page='.$page.'&limit=6" class="bluTxt10px">6</a> |'; }
	if ($limit == 15) { echo ' 15 |'; }
	else { echo ' <a href="'.$PHP_SELF.'?act=$act&page='.$page.'&limit=15" class="bluTxt10px">15</a> |'; } 
	if ($limit == 30) { echo ' 30 |'; }
	else { echo ' <a href="'.$PHP_SELF.'?act=$act&page='.$page.'&limit=30" class="bluTxt10px">30</a> |'; } 
	if ($limit == 60) { echo ' 60'; }
	else { echo ' <a href="'.$PHP_SELF.'?act=$act&page='.$page.'&limit=60" class="bluTxt10px">60</a>'; } 
	echo '</td>
	</tr>';
	
	$sql .= " ORDER BY name ASC LIMIT $page, $limit";
	
	$result = mysql_query($sql);
	while($row = mysql_fetch_array($result))   
	{ 
		$items[] = array(1 => $row['name'], 2 => $row['owner'], 3 => $row['id']);   
	}
	
	// Default # of Columns   
	$numcols = 3;   
	  
	// Number of Items   
	$numitems = count($items);   
	  
	// Number of Rows   
	$numrows = ceil($numitems/$numcols); 
	
	for ($row=1; $row <= $numrows; $row++)   
	{
		$cell = 0;   
		echo ' <tr>'."\n";   
		for ($col=1; $col <= $numcols; $col++)   
		{   
			echo '  <td width="'.round(100/$numcols).'%" class="resTD">'."\n";   
	  
			if ($col===1)   
			{   
				$cell += $row;   
				print '<span class="clubName">
				<a href="community.php?id='.$items[$cell - 1][3].'">'.$items[$cell - 1][1].'</a>
				</span>
				<br />';
				print $items[$cell - 1][2];
			}   
			else
			{   
				$cell += $numrows; 
				print '<span class="clubName">
				<a href="community.php?id='.$items[$cell - 1][3].'">'.$items[$cell - 1][1].'</a>
				</span>
				<br />';
				print $items[$cell - 1][2];
			}   
			echo '  </td>'."\n";   
		}   
		echo ' </tr>'."\n";   
	}
	//-----> Next page processing
	if ($num_rows > $limit)
	{
		echo '<tr><td colspan="3" align="center" class="bluTxt10px">';
		if ($page != 0)
		{ // Don't show back link if current page is first page.
			$back_page = $page - $limit;
			echo("<a href=\"$PHP_SELF?act=$act&page=$back_page&limit=$limit\" class=\"bluTxt10px\"><<&nbsp;</a>\n");
		}
		if (!((($page+$limit) / $limit) >= $pages) && $pages != 1)
		{ // If last page, don't give next link.
			$next_page = $page + $limit;
			echo("<a href=\"$PHP_SELF?act=$act&page=$next_page&limit=$limit\" class=\"bluTxt10px\">&nbsp;>></a>\n");																			
		}
		echo '<br>';
		for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
		{
			$ppage = $limit*($i - 1);																			
			if ($ppage == $page){
			echo("<b>$i</b> \n");} // If current page don't give link, just text.
			else{
			echo("<a href=\"$PHP_SELF?act=$act&page=$ppage&limit=$limit\" class=\"bluTxt10px\">$i</a> \n");}
		}
		echo '</td></tr>';
Thanks in advance :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Pagination - back button problems?

Post by Christopher »

dunc wrote:My problem is that after viewing a club or fish profile, if the user hits back, I get the classic "page cannot be displayed".
Do you do a POST to get to the pagination code? I don't see why this code would give you a warning since it uses GETs. You might want to look into setting headers...
(#10850)
dunc
Forum Newbie
Posts: 13
Joined: Mon Sep 04, 2006 10:17 am

Post by dunc »

I use $_POST to receive the initial search values but then they're all added to session variables.

Once a user has clicked "page 2" of the paginated results, there is no $_POST data being sent - just receiving information from session variables.

Headers?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You could pass the search as a GET (i.e. method="get") or redirect to the first page of results.
(#10850)
dunc
Forum Newbie
Posts: 13
Joined: Mon Sep 04, 2006 10:17 am

Post by dunc »

I did a bit of research on headers like you suggested and ended up trying:

Code: Select all

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0?, false");
session_start();
header("Cache-Control: private");
print headers_sent();
That seems to fix the problem entirely. Is it "safe"/"wise" to use those headers?

Thanks arb.
Post Reply