Jump to content

small pagination issue


svgmx5

Recommended Posts

Hey everyone, i'm having a small issue with my pagination code.

 

Here's what's happening...

 

every time i make a search, it displays the results, however on the pagination it displays all pages...

 

So say there are 4 results on the db, that match the keyword, instead of displaying page 1 or nothing on the pagination links, it displays all number pages (1 2 3 4 5...etc)

 

Not sure what i'm doing wrong, but i hope someone understand what i'm trying to say, and can help me.

 

I'm posting the code that i'm using not sure what to paste, so i'm going to past it all but in sections

 

Thanks

 

<?php
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
	 // cast var as int
	$currentpage = (int) $_GET['currentpage'];
}else{
	 // default page num
	$currentpage = 1;
} // end if	

$rowsperpage = 8;		
$offset = ($currentpage - 1) * $rowsperpage;
//retrieve the info from DB
$sql = "SELECT COUNT(*) FROM locations";
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
$totalpages = ceil($numrows / $rowsperpage);

$state = $_GET['state'];
echo'<div id="search_results">';
echo'<h3>Search Results for '.$state.'</h3>';
$get_results = "SELECT * FROM locations WHERE location_state='$state' ORDER BY location_city DESC LIMIT $rowsperpage OFFSET $offset";
$run_results = mysql_query($get_results) or die(mysql_error());
$num_rows = mysql_num_rows($run_results);
?>

 

<?php
					if ($totalpages > 1){
					/*********** Start the pagination links ********/
						echo "<p>";
						// range of num links to show
						$range = 10;
						// if not on page 1, don't show back links
						if ($currentpage > 1) {
							// get previous page num
							$prevpage = $currentpage - 1;
							// show < link to go back to 1 page
							echo " 
							<a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&state=".$state."&search=Search&page=news'>Previous</a> ";
						} // end if 
						if($result > 0){
							// loop to show links to range of pages around current page
							for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
								// if it's a valid page number...
								if (($x > 0) && ($x <= $totalpages)) {
									// if we're on current page...
									if ($x == $currentpage) {
										// 'highlight' it but don't make a link
										echo " $x";
									  	// if not current page...
									} else {
										 // make it a link
										 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&state=".$state."&search=Search&page=news'>$x</a> ";
									} // end else
								} // end if 
								} // end for
							}else{
								echo "<a href='{$_SERVER['PHP_SELF']}?currentpage=$x&state=".$state."&search=Search&page=news'>1</a>";
						}
							// if not on last page, show forward and last page links        
							if ($currentpage != $totalpages) {
							   // get next page
							   $nextpage = $currentpage + 1;
								// echo forward link for next page 
							   echo " 
								<a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&state=".$state."&search=Search&page=news'>
									Next
								</a> ";
							} // end if
							echo "</p>\n";
							/****** end build pagination links ******/

						}
				?>

 

 

Link to comment
Share on other sites

Hi,

 

It's because the query you're running to calculate the total number of results - and consequently the number of pages to display links to is different to the query you're actually running.

 

Both queries; the one to determine number of pages and the one with the dynamic limit options need to be the same.  I.E:

 

SELECT COUNT(*) FROM locations

 

Should become something like:

 

SELECT COUNT(*) FROM locations WHERE location_state='$state' ORDER BY location_city

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.