hey people,
this script is driving me crazy.
I have this search script Im trying to modify to work with a site. the script is included on a page depending if the address reads ?menu=searchreview .
I put a big IF function round the whole script to say if the form is searched then do the script, if not, then echo a message.
I dont know where Ive gone wrong, but I keep getting random results, the script I have will work one minute and then not the next and if the form is submitted with nothing in it, it still comes back with a result.
heres my script:
<form name="form" action="<? $PHP_SELF; ?>" method="post">
<INPUT TYPE="hidden" NAME="menu" VALUE="searchreview">
<input type="text" name="q" />
<input type="submit" name="action" id="action" value="Search">
</form>
<?php
//Database Table
$dbtable = myTable
//Database Field2
$dbfield1 = title;
$dbfield2 = reviewer;
//Set Limit numbers for result read out
$result1LIMIT = 40;
$result2LIMIT = 100;
// rows to return
$limit = 10;
//If form is submitted
if ($action=="Search") {//-------------------------------------------------------------------------
// Get the search variable from URL
$var = $_POST['q'];
$trimmed = trim($var); //trim whitespace from the stored variable
// check for an empty string and display a message.
if ($trimmed == "") {
echo "<p>Please enter a search...</p>";
}
else { echo '1'; }
// check for a search parameter
if (!isset($var)) {
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}
else { echo '2'; }
//connect to database
mysql_connect("localhost","user","pass");
mysql_select_db("dbname") or die("Unable to select database");
// Build SQL Query
$query = "select * from $dbtable where $dbfield1 like \"%$trimmed%\" order by $dbfield1";
$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 "<p>Sorry, your search: "<b>" . $trimmed . "</b>" returned zero results</p>";
// google
// echo "<p><a href=\"http://www.google.com/search?q="
// . $trimmed . "\" target=\"_blank\" title=\"Look up
// " . $trimmed . " on Google\">Click here</a> to try the
// search on google</p>";
}
else { echo '3'; }
// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
else { echo '4'; }
// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");
// if we have result/s begin to show results set
if ($numrows >= 1) {
echo "<p>You searched for: "<b>" . $var . "</b>"</p>";
echo "<b>Results:</b><br>";
$count = 1 + $s ;
}
else { echo '5'; }
// now display the results returned
while ($row= mysql_fetch_array($result)) {
//Set result character limits
$result1 = substr($row["$dbfield1"],0,$result1LIMIT);
$result2 = substr($row["$dbfield2"],0,$result2LIMIT);
echo "\n<table celpadding=3 cellspacing=0 border=0 width=100%>\n";
echo "<tr bgcolor=#FFFFFF valign=top>\n";
echo "<td width=5% class=searchitemdivide><span class=searchitem>$count.</span><br></td>\n";
echo "<td width=95% class=searchitemdivide><span class=searchtitleitem>".$result1."</span><br>\n";
echo "<span class=searchitem>".$result2."</span><br></td>\n";
echo "</tr>\n";
echo "</table>\n";
$count++ ;
}
$currPage = (($s/$limit) + 1);
//break before paging
echo "<br />";
//do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
if ($numrows >= 1) {
print " <a href=\"$PHP_SELF?menu=searchreview&s=$prevs&q=$var\"><<Prev ".$limit."</a>  ";
}
}
//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 NEXT link
$news=$s+$limit;
if ($numrows >= 1) {
echo " <a href=\"$PHP_SELF?menu=searchreview&s=$news&q=$var\">Next ".$limit." >></a>";
}
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
if ($numrows >= 1) {
echo "<p>Showing results $b to $a of $numrows</p>";
}
}
else {
echo 'Please enter a search.';
}
?>
can anyone see anything that may be causing my problems?
many thanks.