Jump to content

MYSQL search with pagination problem


Hooch

Recommended Posts

Hi everyone. I have a search page made up from a couple tutorial sites I goggled. The results are found correctly. But the pagination is not returningthe next set of results. It just leaves an empty page.Here is my code

<?PHPsession_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Search</title><link href="style.css" rel="stylesheet" type="text/css" media="screen" /></head><body><table width="770" border="1" align="center"  cellpadding="0" cellspacing="0" bordercolor="#E7E7E7" bgcolor="#6B0000" class="light-12">    <tr align="center">	<td height="">		<table width="770" height="109" border="0" align="center"  cellpadding="0" cellspacing="0" background="images/header_675_109.jpg">			<tr align="center">			<td align="left" valign="bottom"> </td>			<td align="right" valign="bottom"> </td>		  </tr>  	  </table>				<table width="100%" border="0" cellpadding="0" cellspacing="0" background="images/links_bg_1_40.jpg">			<tr>			<td width="24.5"> </td>			<td height="40" align="center"><?PHP include 'links_header.php'; ?></td>			<td width="24.5"> </td>		  </tr>		</table>		<table width="100%" height="500" border="0" cellpadding="0" cellspacing="0" class="no-repeat-sketch">		  <tr>			<td align="center" valign="top">			  <p></p>		<table width="100%" border="0" align="center"  cellpadding="0" cellspacing="0" background="" class="light-12">		  <tr><td colspan="3" height="15"> </td></tr>		  <tr>			<td width="35"> </td>			<td align="left">			<form method="POST" action="<?PHP $PHP_SELF;?>">			<input type="text" name="search" size=25 maxlength=25 value="Good Luck" class="txtbox">			<input type="Submit" name="Submit" value="Search" class="txtbox">			</form><?PHPif($_POST['Submit'])	{	 include'config.php';	 // Grab the posted info	 //$search = $_POST["search"];	 session_register("s_search"); // Name field // These must be in quotes " not '	 $s_search = $_POST['search']; 	 $limit = 2;					 	 $query = mysql_query("SELECT * FROM items WHERE 	 lastname LIKE '%$s_search%' OR 	 firstname LIKE '%$s_search%' OR 	 collection LIKE '%$s_search%' OR	 gallery_id LIKE '%$s_search%' OR	 medium LIKE '%$s_search%' OR	 title LIKE '%$s_search%' OR	 price LIKE '%$s_search%' OR 	 item_size LIKE '%$s_search%'");	 		 // Get number of rows that contain search kewords	 $totalrows = mysql_num_rows ($query);	 	 if(empty($page))		 { 		 $page = 1; 		 } 		 	$limitvalue = $page * $limit - ($limit);  	$query  = "	 SELECT * FROM items WHERE 	 lastname LIKE '%$s_search%' OR 	 firstname LIKE '%$s_search%' OR 	 collection LIKE '%$s_search%' OR	 gallery_id LIKE '%$s_search%' OR	 medium LIKE '%$s_search%' OR	 title LIKE '%$s_search%' OR	 price LIKE '%$s_search%' OR 	 item_size LIKE '%$s_search%'	 ORDER BY gallery_id  ASC LIMIT $limitvalue, $limit";		 	$result = mysql_query($query) or die("Error: " . mysql_error());  // Echo the result	  echo "Your query of <span class=\"black-yellow-bg-12\">".$s_search."</span> returned <b>".$totalrows."</b> results.<br><br>";while($r=mysql_fetch_array($result))	{	  	$id=$r["id"];	$firstname=$r["firstname"];	$lastname=$r["lastname"];	$collection=$r["collection"];	$gallery_id=$r["gallery_id"];	$medium=$r["medium"];	$title=$r["title"];	$price=$r["price"];	$size=$r["item_size"];	$description=$r["description"];	$firstname = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $firstname);	$lastname = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $lastname);	$collection = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $collection);	$gallery_id = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $gallery_id);	$medium = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $medium);	$title = preg_replace( "'($s_search)'si" ,  "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $title);	$price = preg_replace( "'($s_search)'si" ,  "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $price);	$size = preg_replace( "'($s_search)'si" ,  "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $size);	$description = preg_replace( "'($s_search)'si" , "<span class=\"black-yellow-bg-12\">".$s_search."</span>" , $description);  			if ($price == 0)			{			$price = "<strong>(Sold)</strong>";			} else {			$price = "<span class=\"light-dark-12\"><strong>Price:</strong></span>$".$price;			} echo "<span class=\"light-dark-12\"><strong>By:</strong></span>".$firstname." ".$lastname."	<span class=\"light-dark-12\">(".stripslashes($collection).")	"?><a href="art2.php?id=<?PHP echo $id?>" class="light-12-link" title="<?PHP echo $description?>"><u>View</u></a><?PHP echo "<br><span class=\"light-dark-12\"><strong>ID#:</strong></span>".$gallery_id."   <span class=\"light-dark-12\"><strong>Medium:</strong></span>".stripslashes($medium)."   <span class=\"light-dark-12\"><strong>Title:</strong></span>".stripslashes($title)."   ".$price."   <span class=\"light-dark-12\"><strong>Size:</strong></span>".$size."<hr>";}	if($page != 1){  		$pageprev = $page--; 		 		echo"<a href=\"$PHP_SELF?page=$pageprev\" class=\"light-12-link\">PREV</a> ";  	}else{ 		echo""; 	} 	$numofpages = $totalrows / $limit;  	 	for($i = 1; $i <= $numofpages; $i++){ 		if($i == $page){ 			echo($i." "); 		}else{ 			echo"<a href=\"$PHP_SELF?page=$i\" class=\"light-12-link\">$i</a> "; 		} 	} 	if(($totalrows % $limit) != 0){ 		if($i == $page){ 			echo($i." "); 		}else{ 			echo"<a href=\"$PHP_SELF?page=$i\" class=\"light-12-link\">$i</a> "; 		} 	} 	if(($totalrows - ($limit * $page)) > 0){ 		$pagenext = $page++; 		$pagenext = $pagenext + 1;		  		echo"<a href=\"$PHP_SELF?page=$pagenext\" class=\"light-12-link\">NEXT</a>";  	}else{ 		echo"";  	} 	} else {	  echo "";	  }?>			</td>			<td width="35"> </td>		  </tr>		</table>			</td>		  </tr>		</table>		<table width="770" height="25" border="0" align="center" cellpadding="2" cellspacing="0">			<tr>				<td width="385"> </td>				<td width="385" align="right" valign="bottom" class="light-10">Copyright info here</td>			</tr>	  </table>		</td>  </tr></table></body></html>

When I mouse over the links to see the next page(s)they seem fine too.I'm hoping this is a common problem and can easily be seen.Thank you for taking the time to look and help.Hooch

Link to comment
Share on other sites

The links don't include any of the search data. $_POST['submit'] is not set after clicking on a link or any of the other post data. You will need to include any of the search information (keywords, etc) on the links as well and check for those in $_GET. You tell it to go to page 2, but you don't tell it page 2 of what.

Link to comment
Share on other sites

Gotcha..makes sense.So I changed this..

echo"<a href=\"$PHP_SELF?page=$pagenext\" class=\"light-12-link\">NEXT</a>";

to this..

echo"<a href=\"$PHP_SELF?page=$pagenext&q=$s_search\" class=\"light-12-link\">NEXT</a>";

No change in my problem from this.Is this as simple as adding the query to the link, or do I need to recode most of the script?Thanks guy. You are a great help as always. Hooch

Link to comment
Share on other sites

if($_POST['Submit'])	{	 include'config.php';	 // Grab the posted info	 //$search = $_POST["search"];	 session_register("s_search"); // Name field // These must be in quotes " not '	 $s_search = $_POST['search'];

in your code you're guiving to $s_search the value of $_POST['search'] so 'search' should be the name for the parameter you post on the link, something like
echo"<a href=\"$PHP_SELF?page=$pagenext&search=$s_search\" class=\"light-12-link\">NEXT</a>";

Link to comment
Share on other sites

Since you're passing things through the querystring obviously you can't rely on $_POST. It would be better to switch your original form from submitting using post to using get, and then just use $_GET throughout the script. Also, don't use the Submit button to determine if the form was submitted, check for the presence of the keyword variable instead.

Link to comment
Share on other sites

Thanks Guy and Obi1.It worked perfect.Now for some reason my links are off.The next and previous stay on the same page when clicked.Also if I manually click (say) page 7 (of 20) it goes to page 7 but the number down in pagination is not unlinked likeit should, it's the previous number. Something is off by 1. Again thanks alot guys. Hooch

Link to comment
Share on other sites

You're decrementing page here:$pageprev = $page--;2 things happen on that line. The value of $page is copied to $pageprev and then $page is decremented by 1. So after that line $page will be 1 less then it was. That makes the next calculation use the wrong value for $page.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...