Jump to content

PHP Search and Results problem


bluedot

Recommended Posts

Hi,

I have the following HTML Forum.

 

<html>
<head>
<title>CSA Quotes Database</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><style type="text/css">
<!--
body {
background-color: #FFFFCC;
}
.style1 {
font-size: 12px;
color: #FF0000;
font-weight: bold;
}
-->
</style></head>

<body>

<table width="802" border=1 align="center">
  <!--DWLayoutTable-->
  <tr> 
    <td width="303" height="240" valign="middle"> <div align="center"><img src="images/csalogotrns.gif" width="277" height="193"></div></td>
    <td width="289" valign="middle"> <div align="center"> 
        <h1>CSA Quotes Database </h1>
      </div></td>
    <td width="186" valign="middle"> <div align="center"><img src="images/jsnlogo.gif" width="180" height="236"></div></td>
  </tr>
  <tr> 
    <td height="71" valign="middle">
<div align="center"><strong><a href="quotes.php">Browse 
        All</a></strong></div></td>
    <td> </td>
    <td> </td>
  </tr>
</table>
<p> </p>
<p> </p>
<form name="form1" method="post" action="quotequery.php">
  <table width="556" border="1" align="center">
    <tr>
      <td width="164"><div align="right">Search In: </div></td>
      <td width="376"><label>
        <select name="conditions" id="conditions">
          <option value="author">Author</option>
          <option value="source">Source</option>
          <option value="quote" selected>Quote</option>
        </select>
        <span class="style1">Source includes the date </span></label></td>
    </tr>
    <tr>
      <td><div align="right"></div></td>
      <td><label></label>
        <p><label></label>
          <label>
          <input type="radio" name="condition" value="contains">
Contains</label>
          <br>
          <label>
          <input type="radio" name="condition" value="exact">
Exact Phrase</label>
          <br>
      </p></td>
    </tr>
    
    <tr>
      <td><div align="right">Keywords:</div></td>
      <td><input name="find" type="text" id="keyword1"></td>
    </tr>
    <tr>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td><label>
        <input type="reset" name="Submit2" value="Reset">
      </label></td>
      <td><input type="submit" name="Submit" value="Submit"></td>
    </tr>
  </table>
</form>
<p> </p>
</body>
</html>

 

That is sent to the following PHP Script

 

  <?php
  
  //$testing = $_POST['find'];
  //echo "<h2>$testing **</h2><br>";
  
//echo "$find ***  $condition *** $exact <br>";
  // Connect to database server
include ("xoops-auth.php");
    $hd = mysql_connect("$host", "$username","$password")
 or die ("Unable to connect");
    mysql_select_db ("$database", $hd)
 or die ("Unable to select database");

$data1 = mysql_query("SELECT * FROM quotes", $hd)
or die(mysql_error());
$rows = mysql_num_rows($data1) 
or die ("Unable to select database");

$origanal = $_POST['find'];
$conditions = $_POST['conditions'];
$condition = $_POST['condition'];

if ($condition == "contains")
{
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find); 
}
//This is the number of results displayed per page
$page_rows = 4;

//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

$data = mysql_query("SELECT * FROM quotes WHERE upper($conditions) LIKE'%$find%' $max", $hd);
if (mysql_num_rows($data) > 0)
{
?>
</p>
<table width="468" border=1 align="center">
  <tr>
    <td width="124" height="71"> </td>
    <td width="206"><div align="center">
      <h3>CSA Quotes Database </h3>
    </div></td>
    <td width="116"> </td>
  </tr>
  <tr>
    <td height="23" colspan="3"><div align="center"><strong><a href="quotes.php">Browse All</a></strong>  <strong><a href="index.html">Home</a>  <a href="events.php">Events</a></strong> </div></td>
  </tr>
</table>
<p>You searched for:<br>
<?php echo "$origanal<br><h3>$rows results</h3><br>"; ?></p>
<p> </p>
<table width="979" border="1" align="center">
  <tr class="row">
    <td>Author</td>
    <td>Source</td>
    <td>Quote</td>
  </tr>

<?php
while ($result_ar = mysql_fetch_assoc($data))
{
?>
<tr <?php if($i%2 == 1){ echo "class='body2'"; }else{echo "class='body1'";}?>>
    <td width="12%" class="style1"><?php echo $result_ar['author']; ?></td>
    <td width="24%" class="style1"><?php echo $result_ar['source']; ?></td>
    <td width="64%" class="style1"><?php echo $result_ar['quote']; ?></td>
  </tr>
<?php
} // end while loop
} // end the if number of rows > 0
else
{
echo "<h2>Sorry, your search did not return any results. <a href='quotessearch.php'>Search Agian</a></h2><br>";
}
?>
</table>
<div align="center">
  <?php
// This shows the user what page they are on, and the total number of pages
echo " --Page $pagenum of $last-- <p>";

// First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <div align='center'><a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a>";
echo " ";
$previous = $pagenum-1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a>";
}

//just a spacer
echo " --- ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo "<a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a></div> ";
}
?>

 

The html form sends the variables to the PHP script via the post and $_POST features. Then the php script limits the results of the search and splits them up between the needed pages (determined by the number of results).

The first page of results works fine. However the second page and any other pages after that get an error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in *path*quotequery.php on line 102

This, i am guessing means the query failed because the variables received from the HTML form are no longer in memory.

 

So my question is how can I get the variables from the form to stay in memory.

 

Thanks in advanced.

 

Link to comment
https://forums.phpfreaks.com/topic/39232-php-search-and-results-problem/
Share on other sites

what is it printing when you test the output of your $_POST vars at the top of the page?

 

It is printing the correct values. They print whatever the search parameters were that the user entered. Except when I go to page two.

I only display 4 results per page, then i have " <prev <<first  last>> next>"  links to go to the next page of results. When i go to the "next" page the search results are gone

 

well you are going to have the problem because you are using POST for your search results and GET for the pages. So when you go to the next page the search criteria are not being sent along with the page.

 

try changing these things

 

Using request will retrieve your variables weather they are POST or GET

$origanal = $_REQUEST['find'];
$conditions = $_REQUEST['conditions'];
$condition = $_REQUEST['condition'];

 

you have not declared $find

$find = strtoupper($origanal);
$find = strip_tags($find);
$find = trim ($find);

 

Then change your links to include the original search criteria

echo " <div align='center'><a href='{$_SERVER['PHP_SELF']}?pagenum=1&find=".$origanal."&conditions=".$conditions."&condition=".$condition."'> <<-First</a>";

 

Go through the rest of the links and add the 3 search criteria

 

Ray

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.