Page 1 of 1

Search Script Issues - HELP!

Posted: Mon Mar 05, 2007 12:21 pm
by kentopolis
I have been able to get everything to work in this script, 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 ... earch+Pets You can visit to see what it is doing.
Here is a sample of the script (if you need, I can provide the entire script):

Code: Select all

<?php
$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 "&nbsp;<span class="styledogsforsale"><a href="$PHP_SELF?s=$prevs&q=$var"><< 
  Prev 5 Results</a></span>&nbsp&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>&nbsp;<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.

Posted: Mon Mar 05, 2007 12:26 pm
by RobertGonzalez
Where are you setting $s?

Posted: Mon Mar 05, 2007 12:31 pm
by kentopolis
I don't know that it is being set... I have wondered about that myself, but didn't even know what to set $s as...I am very new to php and have struggled through this script...
Here is the rest of the code:

Code: Select all

<?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 "";
$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>&nbsp;";
}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>&nbsp;";
}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>&nbsp;";
}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>&nbsp;";
}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 "&nbsp;<span class=\"styledogsforsale\"><a href=\"$PHP_SELF?s=$prevs&q=$var\"><< 
  Prev 5 Results</a></span>&nbsp&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>&nbsp;<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>";
  
?>

Posted: Mon Mar 05, 2007 1:17 pm
by RobertGonzalez

Code: Select all

<?php
  if (empty($s)) {
  $s=0;
  }
?>
This suggests that the script is relying on register_globals being on, which it probably isn't (and should not be). Change that part to

Code: Select all

<?php
  $s = empty($_GET['s']) ? 0 : $_GET['s'];
?>
Also, when posting PHP in the forums, use the PHP button instead of the code button. It makes it easier to read for us. :wink:

Posted: Mon Mar 05, 2007 1:21 pm
by kentopolis
That was the one!!! Thank you so much, I have been wracking my brain over this. I will also use the PHP button from now on, thanks, I appreciate how kind you were in helping!

Posted: Mon Mar 05, 2007 1:24 pm
by RobertGonzalez
You got it. Post back as needed.

Posted: Mon Mar 05, 2007 8:21 pm
by feyd
$PHP_SELF won't exist without register globals either. Often it's not a good idea to use it.

Posted: Mon Mar 05, 2007 10:13 pm
by RobertGonzalez
Man, I hadn't even seen that one.