Jump to content

Search Script


Diegar
 Share

Recommended Posts

I apologize in advance for dumping a whole script at once, but i have a feeling it would be requested eventually. Besides, i don't know where the issue is stemming from. This is a pretty basic search script. It works, meaning it gives me results, but has a limit of 10. If the search goes beyond 10 results, it will show "Next >>". Problem is, when you click on "Next >>", the pages refreshes, but the same first 10 results remain. It has to be something simple, but i can't seem to locate where the issue is. Anyone able to help?

<?php    $var = @$_GET['q'];  $trimmed = trim($var);$limit=10;if ($trimmed == "")  {  echo "<p>Please enter a search...</p>";  exit;  }if (!isset($var))  {  echo "<p>We dont seem to have a search parameter!</p>";  exit;  }require("dbconfig.php");$query = "SELECT * FROM beer WHERE name LIKE \"%$trimmed%\"    ORDER BY name"; $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults);if ($numrows == 0)  {  echo "<h4>Results</h4>";  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";  }  if (empty($s)) {  $s=0;  }  $query .= " limit $s,$limit";  $result = mysql_query($query) or die("Couldn't execute query");echo "<p>You searched for: "" . $var . ""</p>";echo "Results";$count = 1 + $s;  while ($row= mysql_fetch_array($result)) {  $title = $row["name"];  echo "<br>$count.) $title";  $count++;  }$currPage = (($s/$limit) + 1);  echo "<br />";  if ($s>=1) { // bypass PREV link if s is 0  $prevs=($s-$limit);  print " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<   Prev 10</a>  ";  }  $pages=intval($numrows/$limit);  if ($numrows%$limit) {  $pages++;  }  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {  $news= $s+ ($limit);  echo " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";  }$a = $s + ($limit);  if ($a > $numrows)	  { $a = $numrows; }  $b = $s + 1;  echo "<p>Showing results $b to $a of $numrows</p>";?>

Link to comment
Share on other sites

Right, you don't want to use register globals, but it looks like your script is relying on it being enabled. You need to explicitly get the $s variable from the querystring like you do with the q variable.
Okay, but i don't see where the s variable is getting lost. It isn't going into a function.. and the s in the <a href> isn't referring to the $s variable. This may sound unintelligible, but i am trying to work through it.From what i see, q = $var which is fine, but i don't see anything that makes it more important than s = $news...I am lost.. sorry
Link to comment
Share on other sites

You can make it a global variable, but that's not what you're trying to do here. A global variable is a variable that exists in the "normal" scope, and also function scopes. So you can use the one variable in several functions.

$var = "test";function func1(){  global $var;  echo isset($var); //prints "true"  echo $var; //prints "test"}function func2(){  echo isset($var); //prints "false"}

The global variable can be used in any scope while the script is running, but it's not going to still be in existence for other scripts. You can use the session if you want to pass a variable to another page without using $_GET or $_POST, but it seems like all you need to do here is put this at the top of your script:$s = (isset($_GET['s']) ? $_GET['s'] : 0);

Link to comment
Share on other sites

Okay... same script, new issue. Now when i search for something in my DB, it works perfectly, but when i search for "fdfdfaf", it tells me there are no results to display, but still adds the "Next >>" to the bottom and says 1 to 0 of 0 results. If i click next, it says 11 to 0 of 0 results and adds the "<< Prev" Any idea what is wrong?

Link to comment
Share on other sites

This is the test to show the next link:if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {You might want to think through that and see if it's doing what you want.
Got it! I changed:
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

Into:

if (!((($s+$limit)/$limit)==$pages) && $pages!=1 && $pages!=0) {

... then it worked almost perfectly. The only issue left was that if you searched for something not in the database (ie "fdfdfdfd"), it would display, "Showing results 1 to 0 of 0"This bugged me, and if there are no results, it is a dumb line to display, so i added an IF statement to the code that displays this information, as so:

if ($numrows != 0){	$a = $s + ($limit);		  if ($a > $numrows)			{ $a = $numrows; }		  $b = $s + 1;		  echo "<p>Showing results $b to $a of $numrows</p>";				  }

Now it seems to be working as intended. Thanks for your guidance!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...