Hello,

I got my search engine working and it does show the number of results including the number of pages it should have. But I'm having trouble outputting the results when I click next. When I click the next page it doesn't show any results.

<?PHP
global $search_term;
global $location_term;
global $results;
function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$connection_string = "connectionstring.php";
require_once($connection_string);
mysql_select_db("chef") or die ( 'Unable to select database.' );
$RESULTS_LIMIT=10;
if(isset($_GET['search_term']) && isset($_GET['location_term']) && isset($_GET['search_button']))
{
      $search_term = $_GET['search_term'];
	  $location_term = $_GET['location_term'];
	  
    if(!isset($first_pos))
    {
        $first_pos = "0";
    }

    $start_search = getmicrotime();
    $sql_query = mysql_query("SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term')");

    if($results = mysql_num_rows($sql_query) != 0)
            {
                $sql =  "SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term') LIMIT $first_pos, $RESULTS_LIMIT";
                  $sql_result_query = mysql_query($sql);   
            }
    else
            {
                  $sql = "SELECT * FROM searchengine WHERE (product LIKE '%".mysql_real_escape_string($search_term)."%' AND location LIKE '%".mysql_real_escape_string($location_term)."%') ";
                  $sql_query = mysql_query($sql);
                  $results = mysql_num_rows($sql_query);
                  $sql_result_query = mysql_query("SELECT * FROM searchengine WHERE (product LIKE '%".$search_term."%' AND location LIKE '%".$location_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
            }
    $stop_search = getmicrotime();
    $time_search = ($stop_search - $start_search);
}
?>
<?PHP
global $first_pos;
global $sites;
if($first_pos > 0)
{
  $back=$first_pos-$RESULTS_LIMIT;
  if($back < 0)
  {
    $back = 0;
  }
  echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$back' ></a>";
}
if($results>$RESULTS_LIMIT)
{
  $sites=intval($results/$RESULTS_LIMIT);
  if($results%$RESULTS_LIMIT)
  {
    $sites++;
  }
}
for ($i=1;$i<=$sites;$i++)
{
  $fwd=($i-1)*$RESULTS_LIMIT;
  if($fwd == $first_pos)
  {
      echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '><b>$i</b></a> | ";
  }
  else
  {
      echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '>$i</a> | ";   
  }
}
if(isset($first_pos) && $first_pos < $results-$RESULTS_LIMIT)
{
  $fwd=$first_pos+$RESULTS_LIMIT;
  echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd ' > >></a>";
  $fwd=$results-$RESULTS_LIMIT;
}
?>

Can anyone help me out on this?

Also, I think my search doesn't get passed to the next page. Any idea how to preserve that?

Thanks

any suggestions?

You should read firstpos from $_GET; at the start of the page.

If you want to keep your results without querying, then you need to add them to a $_SESSION variable. Personally I think it is better to re-query the database.

I can see in my URL that it does have the search term for the next page. Can you help me out as to where I should put the $_GET? Sorry I"m a newbie at php.

Thanks

Before line 19 in the first, before 4 in the second:

$firstpos = $_GET['firstpos'];

I put that code in the lines you mentioned but now I get this error

"Undefined index: first_pos in C:\wamp\www\nootri\includes\form.php on line 19"

And the second page still doesn't have a result.

Thanks for the help!

Probably becuase the second issue from my first post is still open: If you want to keep your results without querying, then you need to add them to a $_SESSION variable. Personally I think it is better to re-query the database.

If you think that its better to re-query the database, I would take your word for it since I'm a beginner at this. I'm not sure which way is better. But my problem is that I'm not sure how to code the script I have to make it work. I've been using example tutorial scripts and just modified it to my database.

Can you help me with what I need to do?

Thanks

You can search this forum for even more examples.

I second that, this is kind of FAQ, and a lot of practical examples.
Just pick one and post any problem you get (There is nice example from PHPMyCoder)

I know. I have used an example. Thats why I'm posting here because I have a problem to my script.

I still don't know why my search query is not getting passed to the next page. I have the search term in my URL but it just shows an empty result.

Well I just figured it out.

I had to add search_button=Search in my link

echo "<a href='index2.php?search_term=".stripslashes($search_term)."&search_button=Search&first_pos=$back' >

Thanks for helping @pritaeas

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.