Jump to content

Problem with search script...


kentopolis

Recommended Posts

I am having an issue with this script

  <?php

  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=5; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","pets_puppylister","fiveft12"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("pets_puppylist") or die("Unable to select database"); //select which database we're using


// Build SQL Query  
$query = "select * from dogs where ptype like \"%$trimmed%\" AND pshow = 'y'
  order by did desc"; // EDIT HERE and specify your table and field names for the SQL query

$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";

  }

//  determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: "" . $var . ""</p>";

// begin to show results set
echo "Results";
$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["ptype"]; 
  $email = $row["email"];
  $did = $row["did"];
  ///////////////////////////
	$gender = $row["gender"];
	$price = number_format($row["price"], 2,'.',',');
	$qty = $row["qty"];
	$dob_month = $row["dob_month"];
	$dob_day = $row["dob_day"];
	$p_pound = $row["p_pound"];
	$p_oz = $row["p_oz"];
	$p_date = $row["p_date"];
	$link = $row["link"];
	$p_num = $row["p_num"];
	$address = $row["address"];
	$comment = $row["comment"];
	$avail_month = $row["avail_month"];
	$avail_day = $row["avail_day"];
	//////////////////////////////


echo "<p><span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p><span class=\"styledogsforsale\">$title for Sale - ID #$did</span><br><br>D.O.B. - $dob_month $dob_day<br>$qty $gender $title<br><span class=\"style40\">Price: $$price<br>Ad Expires $avail_month $avail_day<br>$Comment</span><p><span class=\"styledogsforsale\">Contact Information:</span><br>$address<br><a href=mailto:$email>$email</a><br>$p_num <br><a href=\"$link\" target=\"_blank\">$link</a>
                        <br>"; 
if(file_exists('admin/uploads/'.$did.'.jpg')){
echo "<a href=\"admin/uploads/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesize.php?did=$did&h=100\" border=\"0\"></a> ";
}else{
echo "";
};
if(file_exists('admin/uploads/a/'.$did.'.jpg')){
echo "<a href=\"admin/uploads/a/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizea.php?did=$did&h=100\" border=\"0\"></a> ";
}else{
echo "";
};
if(file_exists('admin/uploads/b/'.$did.'.jpg')){
echo "<a href=\"admin/uploads/b/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizeb.php?did=$did&h=100\" border=\"0\"></a> ";
}else{
echo "";
};
if(file_exists('admin/uploads/c/'.$did.'.jpg')){
echo "<a href=\"admin/uploads/c/$did.jpg\" target=\"_blank\"><img src=\"admin/imagesizec.php?did=$did&h=100\" border=\"0\"></a> ";
}else{
echo "";
};

"

   <p>";
                     
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  //  we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <span class=\"styledogsforsale\"><a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 5 Results</a></span>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

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

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

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give  link
  $news=$s+$limit;

  echo "<p> <span class=\"styledogsforsale\"><a href=\"$PHP_SELF?s=$news&q=$var\">Next 5 Results >></a></span>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p><span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p>Showing results $b to $a of $numrows</p>";
  
?>

I have gotten everything to work on it, except, when it goes to create a new page with the next set of results, it turns up the first page of results.  I have gone through the code several times and can't seem to get anywhere.  Here is a sample search using the script http://www.petsforsalecs.com/search.php?q=a&Submit=Search+Pets You can visit to see what it is doing.  Thank you so much!

I think specifically the issue is in this part somewhere, but I just don't know enough about the language to figure it out:

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";

  //  we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print " <span class=\"styledogsforsale\"><a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 5 Results</a></span>&nbsp ";
  }

// calculate number of pages needing links
  $pages=intval($numrows/$limit);

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

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

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give  link
  $news=$s+$limit;

  echo "<p> <span class=\"styledogsforsale\"><a href=\"$PHP_SELF?s=$news&q=$var\">Next 5 Results >></a></span>";
  }

$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p><span class=\"style52\">- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - </span><p>Showing results $b to $a of $numrows</p>";
  
?>

Here is a complete working version of the script http://www.designplace.org/search.php?s=0&q=p which I got from http://www.designplace.org/scripts.php?page=1&c_id=25 and have manipulated.  Thanks for all your help, this forum has been a lifesaver!

Link to comment
https://forums.phpfreaks.com/topic/41302-problem-with-search-script/
Share on other sites

I don't know that my question is very clear. 

So here it is.  Why does this script continue to show the same results when you click "next 5 results >>", it should show me the next set of results but instead it shows the same thing again.  I can't figure it out, please help...

Archived

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

×
×
  • 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.