Link to home
Start Free TrialLog in
Avatar of LeighJor
LeighJorFlag for Afghanistan

asked on

PHP search result page not paginating

Hi this script works ok for the first page but on the next loop "trimmed ="" " is true so all i get is" Please enter a search..". I'm not sure of the code i need so that results 2 to 4 will be displayed. Can some one please assist. I can paginate without doing search ok. It seems I 'm losing the search var.
Cheers and thanks in advance
Sigh ..here is the code:
<?php
// Get the search variable from URL
     $var = @$_POST['q'] ;
     $trimmed = trim($var) ;//trim whitespace from the stored variable
     echo $trimmed;
// rows to return
     $limit=2;

// check for an empty string and display a message.
     if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
//echo $trimmed;
// check for a search parameter
     if (!isset($var)){
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
    $host = "";
    $dtitle = "";
    $username = "";
    $password = "";
    $conn = mysql_connect($host, $username, $password);
    mysql_select_db($dtitle,$conn);
// Build SQL Query  
       $query = "select * from journal where title like \"%$trimmed%\"  
  order by updated ASC"; //

    $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 "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  }
      if (empty($s))
  {
  $s=0;
  }
// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
       while ($row= mysql_fetch_array($result)) {
  $title = $row["title"];
  echo "$count.)&nbsp;$title" ;
  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "<br />";
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"searchscript.php?s=$prevs&q=$var\">&lt;&lt;
  Prev 10</a>&nbsp&nbsp;";
  }
// 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;
  echo "&nbsp;<a href=\"searchscript.php?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
?>

<!-- © http://www.designplace.org/ -->

</body>
</html>
Avatar of Cedric Obinna A.
Cedric Obinna A.
Flag of Nigeria image

Hello LeighJor,

Change the logical test: from if ($trimmed == "") to if ($trimmed == "" && RECORDSET NOT EMPTY). Replace RECORDSET NOT EMPTY with the appropriate recordset variable you are using. But the main thing is use a logical AND in the if statement that echoes "Plese enter a search". Hope that helps.

Regards,

cedlinx
Avatar of LeighJor

ASKER

Not sure what you mean by : Replace RECORDSET NOT EMPTY with the appropriate recordset variable .
What do you mean by recordset   I know Ithink  taht it is a set representing results of query. What variable holds my recordset what is it
 $trimmed == "" && isset RECORDSET NOT EMPTY?
Cheers
Also if I rem out the if ($trimmed == "") condition then   if (!isset($var)) echoes its message so I guess i need some code to let it know that $var has been sent. Thinking out aloud.
A couple of thing on this line:

$var = @$_POST['q'] ;
Firstly, what the @ for? Surely you just want $_POST['q'];
Secondly, the link to the next page is a GET query not a POST. I presume the first page POSTs the data in, but subsequent queries call it as part of the URL. Change this line therefore to:

$var = $_REQUEST['q'];

this will load both POST and GET variables.

Matt
Are you using a database?  What I meant by RECORDSET NOT EMPTY is: Instead of checking for an empty $trimmed alone, LET that IF STATEMENT ALSO CHECK whether the record already contains data/ rows. Of course if it contains data or rows, it means $var has been sent.

From your last post though it seems $var is lost after the first loop, so look for a way to retain the values of $var and $trimmed. You may use hidden textboxes which would repost them from page to page OR You may store them in session variables. Hope I am clearer now
Don't worry Matt, I just went through your code and I see you are not using a database. Let me scrutinize it a litlle and I'd get back to you
There's a database being used in there alright...

I just tested the code, there's problems with the limiting functions, but as far as the search variable being passed about goes, its as I said, a simple place of replacing $var = @$_POST['q']  with $var = $_REQUEST['q']

Changed $var as suggested. Changed if ($trimmed == "")  to  if ($trimmed == "" && Snumrows == "")
The result was( "h" is passed form on another page and there are 4 rows where title contains h)

h
You searched for: "h"

Results1.) jkhgkjh2.) hory
 Next 10 >>
Showing results 1 to 2 of  4

When I click on next the new url  .../asearchscript.php?s=2&q=h appears but displays the same as the first page. If you know what I mean....and thanks by the way, this is really cheesing me off. I mean I can do the search without paginating. And paginate without search. So i guess again it has something to do with retaining or letting go of a value.  



First of all, sorry for my last post... I actually meant to say I see that you use a database. I've gone through your code and I think the problem is with this.  When you request data from a FORM, you can use $var = @$_POST['q'] but when you request from a Querystring or URL, you use $var = @$_GET['q']. So in this case, Modify your code so that if the page is being accessed the first time, it will execute the $var = @$_POST['q'] but if it is the NOT the first time, it will execute the $var = @$_GET['q']. I hope you understand me.
SOLUTION
Avatar of Cedric Obinna A.
Cedric Obinna A.
Flag of Nigeria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One way to do it is: Check if the submit button was clicked like this... Assuming the name of your submit button is SEARCH, then you'd do this: if (isset($_POST['SEARCH'])) { $var = @$_POST['q'] } else {$var = @$_GET['q']} 2. You can store data in a session on first visit and use that in subsequent ones.
I replaced
 / Get the search variable from URL
     $var = @$_POST['q'] ;
with
if (isset($_POST['SEARCH'])) {
   $var = @$_POST['q'];
   } else
   {
   $var = @$_GET['q'];
   }  
Did not work though.  This time $var was empty  Is this where the code should go.  Cheers
Does this looping look right?
$count = 1 + $s ;
       while ($row= mysql_fetch_array($result)) {
  $title = $row["title"];
  echo "($count.)&nbsp;$title" ;
  $count++ ;
  }
Hi LeighJor,

What is the @ for? It should just be $_POST or $_GET or $_REQUEST - no @ needed. That I think is what's causing the problems.

As I said yesterday I noticed a few problems with the LIMIT functions, so I've had to make a few amendments on these, but here's a working sample of your code using $_REQUEST which takes the initial FORM and subsequent GET variables. There's a sample POST form included in this script too if there's no search supplied, so just add your database connection information in where you took it out and run it as it is.

Matt
<?php
// Get the search variable from URL
     $var = $_REQUEST['q'] ;
     $trimmed = trim($var) ;//trim whitespace from the stored variable
 
// rows to return
     $s=$_REQUEST['s'];
     $limit=10;
     $lower_limit=$_REQUEST['s'];
     if (!$lower_limit){$lower_limit="0";}
     if (!$s){$s=0;}
 
// check for an empty string and display a message.
     if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="q">
<input type="submit" value="go">
</form>
<?
  exit;
  }
//echo $trimmed;
// check for a search parameter
     if (!isset($var)){
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
 
    $host = "";
    $dtitle = "";
    $username = "";
    $password = "";
    $conn = mysql_connect($host, $username, $password);
    mysql_select_db($dtitle,$conn);
// Build SQL Query
       $query = "select * from filter_keys where value like \"%$trimmed%\"  order by id ASC"; //
 
    $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 "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="q">
<input type="submit" value="go">
</form>
<?
  }
      if (empty($s))
  {
  $s=0;
  }
// get results
  $query .= " limit $lower_limit,$limit";
        print $query . "<p>";
  $result = mysql_query($query) or die("Couldn't execute query" . mysql_error());
 
// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
// begin to show results set
echo "Results";
$count = 1 + $s ;
// now you can display the results returned
       while ($row= mysql_fetch_array($result)) {
  $title = $row["name"];
  echo "$count.)&nbsp; $title <br>\n" ;
  $count++ ;
  }
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"testing.php?s=$prevs&q=$var\">&lt;&lt;
  Prev 10</a>&nbsp&nbsp;";
  }
// 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;
  echo "&nbsp;<a href=\"testing.php?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
?>

Open in new window

Thanks Matt for your time
Almost there I hope. I think s has to be initialised somewhere or sme thing along those lines. The next and prev seem to think that s = 0 (i think thats it anyway)
I also changed my query as suggested to
$query = "select * from journal where title like \"%$trimmed%\"  order by updated ASC";
Result was:

Notice: Undefined index: s in C:\htdocs\jim\testing.php on line 8
Notice: Undefined index: s in C:\htdocs\jim\testing.php on line 10
select * from journal where title like "%h%" order by updated ASC limit 0,10

You searched for: "h"

Results1.) jkhgkjh2.) hory3.) hory4.) hory

Showing results 1 to 4 of 4

regards leighjor
OK I just suppressed error reporting and it works fine. Not pretty yet but it works OK. Must be some way though so that s does not have to be read first time around
Anyway
Thanks very much Matt 400 and Cedlinx 100
leighjor
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial