PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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):
<?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 " <span class="styledogsforsale"><a href="$PHP_SELF?s=$prevs&q=$var"><<
Prev 5 Results</a></span>  ";
}
// 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 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:
<?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> ";
}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>  ";
}
// 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>";
?>
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!