hey people,
I have this search script which is working fine, except for the paging links at the bottom.
the problem is, the links are carrying on the $var meaning, but I am not collecting that information on the next page, so the script thinks a search has not been carried out.
i know id have to use $var = $GET['q']; to retrieve the q value from the address but by replacing the $POST with $_GET, my search will not work in the first place to even get as far as reading out the links.
heres my script:
<table width="100%" cellpadding="2" cellspacing="0"><tr><td>
<form name="form" action="<? $PHP_SELF; ?>" method="post">
<INPUT TYPE="hidden" NAME="menu" VALUE="searchreview">
<input type="text" name="q" size="50"/>
<input type="submit" name="action" id="action" value="Search">
</form>
</td></tr></table>
<?php
//Database Table
$dbtable = myTable;
//Database Field2
$dbfield1 = 'title';
$dbfield2 = 'review';
//Set Limit numbers for result read out
$result1LIMIT = 40;
$result2LIMIT = 100;
// rows to return
$limit = 1;
// 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 "Please enter a search...";
}
else {
//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
if ($numrows == 0) {
echo "<p>Sorry, your search: "<b>" . $trimmed . "</b>" returned zero results</p>";
}
// next 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");
// 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 ;
}
// 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><a href=?menu=showreview&id=".$row['id'].">".$result1."</a></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>";
}
}
?>
has anyone got any ideas on how to get around this problem?
many thanks.