Hello, all! Ok, here is the situation. I have this simple search function where the user can choose from a number of search types(i.e. by city by name by ID) and then enter in the text that they want to search for. This is all done through a form where the variables are $searchtype and $searchterm.
Now, I don't want an unlimited number of results to show on the same page so I added some code(that someone had submitted on a thread here) that would limit the number of results per page and add links for the rest of the result set.
The problem is that when the user clicks on any of the links to show other parts of the result set, the results don't show. That's because the code looks for $searchtype and $searchterm, post variables from the form.
I tried using session variables which works except that whenever the user searches it returns the result set that he/she first searched for, and I can't figure out how to destroy the session variables when a new search is initiated.
Here's the code:
<?
ob_start();
include ("include/includes.php");
if(session_is_registered("sess_searchterm")) { }
else {
session_register("sess_searchterm", "sess_searchtype", "sess_submit");
$sess_searchtype = $searchtype;
$sess_searchterm = $searchterm;
$sess_submit = $submit;
}
check_login_status($login_status);
ob_end_flush();
?>
some html here
<?
$submit = $sess_submit;
if ($submit)
{
$searchtype = $sess_searchtype;
$searchterm = $sess_searchterm;
trim($searchterm);
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Please go back and try again.";
exit;
}
$searchterm = addslashes($searchterm);
$pagelimit = "1";
// run query (change yourtable to the name of your table)
$strSQL = mysql_query("select * from tblinfo where ".$searchtype." like '%".$searchterm."%'");
// count number of matches
$totalrows = mysql_num_rows($strSQL);
// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
$pagenums = ceil ($totalrows/$pagelimit);
// if no value for page, page = 1
if ($page==''){
$page='1';
}
// create a start value
$start = ($page-1) * $pagelimit;
// blank matches found
//echo "<b>" . $totalrows . " matches found</b><br>\n";
// Showing Results 1 to 1 (or if you're page limit were 5) 1 to 5, etc.
$starting_no = $start + 1;
if ($totalrows - $start < $pagelimit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
$end_count = $start + $pagelimit;
}
echo "Results $starting_no to $end_count shown.<br>\n";
// create dynamic next, previous, and page links
/* lets say you're set to show 5 results per page and your script comes out with 7 results.
this will allow your script to say next2 if you're on the first page and previous5 if you're on the second page. */
if ($totalrows - $end_count > $pagelimit) {
$var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
$var2 = $totalrows - $end_count;
}
$space = " ";
/* output your data wherever you'd like.
// $strSQL = mysql_query("SELECT * FROM $table LIMIT $start,$pagelimit");
// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results
/* now you can do whatever you'd like with this query. it will only output one page at a time. change the $pagelimit variable to whatever to output more than 1 result per page. */
$query= "select * from tblinfo where ".$searchtype." like
'%".$searchterm."%' limit ".$start.",".$pagelimit."";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
for($i=0; $i < $num_results; $i++)
{
$row = mysql_fetch_array($result);
$fanclubno = htmlspecialchars( stripslashes($row["infFanclubNo"]));
$name = htmlspecialchars( stripslashes($row["infName"]));
$city = htmlspecialchars( stripslashes($row["infCity"]));
$state = htmlspecialchars( stripslashes($row["infState"]));
$postal = htmlspecialchars( stripslashes($row["infPostal"]));
$country = htmlspecialchars( stripslashes($row["infCountry"]));
$member_list .= sprintf ("\t\t\t\t\t\t\t<TR valign=\"top\">\r\n");
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">%s</font></TD>\r\n", $name);
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">%s</font></TD>\r\n", $fanclubno);
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">%s</font></TD>\r\n", $city);
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">%s</font></TD>\r\n", $state);
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\">%s</font></TD>\r\n", $country);
$member_list .= sprintf ("\t\t\t\t\t\t\t\t<TD><font size=\"1\" face=\"Verdana, Arial, Helvetica, sans-serif\"><A HREF=\"profile.php?fanclubno=%s\">Profile</A></TD>\r\n", $fanclubno);
$member_list .= sprintf ("\t\t\t\t\t\t\t</TR>\r\n");
}
// $member_list .= "\t\t\t\t\t\t\t\t\t</TABLE>\r\n";
}
?>
<?=$member_list; ?>
</table>
<p>
<?
// previous link (make sure to change yourpage.php to the name of your page)
if ($page>1) {
echo "« <a href='search_results.php?$searchtype=$searchterm&&page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";
}
// dynamic page number links (make sure to change yourpage.php to the name of your page)
for ($i=1; $i<=$pagenums; $i++) {
if ($i!=$page) {
echo " <a href='search_results.php?$searchtype=$searchterm&&page=$i' class=main>$i</a>";
}
else {
echo " <b class='red'>$i</b>";
}
}
// next link (make sure to change yourpage.php to the name of your page)
if ($page<$pagenums) {
echo "" . $space . $space . $space . $space . " <a href='search_results.php?$searchtype=$searchterm&&page=".($page+1)."' class=main>Next " . $var2 . "</a> »";
}
?>
<form method="post" action="search_results.php">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="10">
<tr>
<td>
<p align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Enter
key words:</font></p>
<p align="center"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<input name="searchterm" type=text>
</font></p>
</td>
</tr>
<tr>
<td>
<p align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">Choose
search type:</font></p>
<p align="center"> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">
<select name="searchtype">
<option value="infName" selected>Name</option>
<option value="infFanclubNo">Fanclub Number</option>
<option value="infCity">City</option>
<option value="infState">State</option>
<option value="infPostal">Postal/Zip Code</option>
<option value="infCountry">Country</option>
<option value="infEmail">E-mail</option>
<option value="infHomepage">Homepage URL</option>
<option value="infAOLIM">AOL IM</option>
</select>
</font></p>
</td>
</tr>
<tr>
<td height="43">
<div align="center">
<input name=submit type=submit value="Search">
</div>
</td>
</tr>
</table></form>
I included the search form if that helps. I've been stumped with this one for a while. 😕