Page 1 of 1

Search Results lose filter after first page

Posted: Fri Jan 07, 2011 7:33 pm
by sHuRuLuNi
Hello again,

I have another problem. I have a search form, which searches for different data (name, street ...) and a results page which outputs the results.

I have already built the pagination and it seems the search finds the CORRECT count of results (e.g. if I search for "Smith" it says it found 39 Smiths in the DB, which is indeed correct), but it only shows all Smiths in the first page (e.g. 15 results) but if I click on "next" it shows unfiltered results (e.g. it shows any results from DB which have nothing to do with the search word "Smith").
It still outputs the correct number of pages and count (e.g. a total of 39 results) but only the first 15 results are "Smith" while all the rest is just a variety of other names from the DB.

Here is the code of the results.php page:

Code: Select all

$maxRows_Recordset1 = 15;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_skajclients, $skajclients);
$query_Recordset1 = "SELECT * FROM users WHERE users.emri LIKE '".$_POST["Keywords"]."%' OR users.rruga LIKE '".$_POST["Keywords"]."%' OR users.mbiemri LIKE '".$_POST["Keywords"]."%' OR users.link_me_kodet LIKE '".$_POST["Keywords"]."%' OR users.karta LIKE '".$_POST["Keywords"]."%' OR users.vendi LIKE '".$_POST["Keywords"]."%'";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $skajclients) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

$queryString_Recordset1 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_Recordset1") == false && 
        stristr($param, "totalRows_Recordset1") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_Recordset1 = sprintf("&totalRows_Recordset1=%d%s", $totalRows_Recordset1, $queryString_Recordset1);


.... HTML ....



    <?php do { ?>
    <table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
      
      <tr>
      <td width="20%" class="style4"><?php echo $row_Recordset1['emri']; ?></td>
      <td width="20%" class="style4"><?php echo $row_Recordset1['mbiemri']; ?></td>
      <td width="20%" class="style4"><?php echo $row_Recordset1['vendi']; ?></td>
      <td width="20%" class="style4"><?php echo $row_Recordset1['karta']; ?></td>
      <td align="right" class="style3"><a href="skaj_clients.php?mod=abonimi&id=<?php echo $row_Recordset1['id']; ?>">Hape>></a></td>
    </tr>
      <tr>
        <td colspan="5"><hr /></td>
    </tr>
    </table>
      <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
      <table border="0" width="50%" align="center">
        <tr>
          <td width="23%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, 0, $queryString_Recordset1); ?>"><img src="First.gif" border=0></a>
                <?php } // Show if not first page ?>
          </td>
          <td width="31%" align="center"><?php if ($pageNum_Recordset1 > 0) { // Show if not first page ?>
                <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, max(0, $pageNum_Recordset1 - 1), $queryString_Recordset1); ?>"><img src="Previous.gif" border=0></a>
                <?php } // Show if not first page ?>
          </td>
          <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, min($totalPages_Recordset1, $pageNum_Recordset1 + 1), $queryString_Recordset1); ?>"><img src="Next.gif" border=0></a>
                <?php } // Show if not last page ?>
          </td>
          <td width="23%" align="center"><?php if ($pageNum_Recordset1 < $totalPages_Recordset1) { // Show if not last page ?>
                <a href="<?php printf("%s?pageNum_Recordset1=%d%s", $currentPage, $totalPages_Recordset1, $queryString_Recordset1); ?>"><img src="Last.gif" border=0></a>
                <?php } // Show if not last page ?>
          </td>
        </tr>
      </table>
      <p>&nbsp;      </p>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Thank you for your help.

Re: Search Results lose filter after first page

Posted: Sat Jan 08, 2011 8:45 pm
by sHuRuLuNi
Here is a weird thing:

I have another page, where a similar query is ran, and it works flawlessly!

As you can see, it uses absolutely the same code, yet in the previous one it doesn't work, while here I can click on next page and it shows the correct results.

I have now literally for an hours compared the code in both pages, and maybe I am blind but I can't find any difference whatsoever (except for variable names of course).



Code: Select all

$maxRows_rsSkadon = 4;
$pageNum_rsSkadon = 0;
if (isset($_GET['pageNum_rsSkadon'])) {
  $pageNum_rsSkadon = $_GET['pageNum_rsSkadon'];
}
$startRow_rsSkadon = $pageNum_rsSkadon * $maxRows_rsSkadon;

mysql_select_db($database_skajclients, $skajclients);
$query_rsSkadon = "SELECT * FROM users WHERE users.abo_skadon >= CURDATE() AND users.abo_skadon <= CURDATE() + 14 ORDER BY users.abo_skadon";
$query_limit_rsSkadon = sprintf("%s LIMIT %d, %d", $query_rsSkadon, $startRow_rsSkadon, $maxRows_rsSkadon);
$rsSkadon = mysql_query($query_limit_rsSkadon, $skajclients) or die(mysql_error());
$row_rsSkadon = mysql_fetch_assoc($rsSkadon);

if (isset($_GET['totalRows_rsSkadon'])) {
  $totalRows_rsSkadon = $_GET['totalRows_rsSkadon'];
} else {
  $all_rsSkadon = mysql_query($query_rsSkadon);
  $totalRows_rsSkadon = mysql_num_rows($all_rsSkadon);
}
$totalPages_rsSkadon = ceil($totalRows_rsSkadon/$maxRows_rsSkadon)-1;

$queryString_rsSkadon = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_rsSkadon") == false && 
        stristr($param, "totalRows_rsSkadon") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_rsSkadon = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_rsSkadon = sprintf("&totalRows_rsSkadon=%d%s", $totalRows_rsSkadon, $queryString_rsSkadon);

Re: Search Results lose filter after first page

Posted: Mon Jan 10, 2011 5:21 am
by sHuRuLuNi
bump
:oops:

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 12:22 am
by sHuRuLuNi
No one?

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 6:21 am
by kalpesh.mahida
Can update you html code (code of your search form)?

I think something wrong with $_POST["Keywords"]

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 6:36 am
by sHuRuLuNi
kalpesh.mahida wrote:Can update you html code (code of your search form)?

I think something wrong with $_POST["Keywords"]
Hello,

Here is the html form:

Code: Select all

<form name="form1" method="post" action="skaj_clients.php?mod=results">
<strong class="nentitujt style1">K&euml;rko klientat:</strong>
<input name="Keywords" type="text" id="Keywords" size="15"> 
<input name="imageField" type="image" src="images/m12.gif">
          </form>
It seems fine to me actually ... and it works the first time, e.g. when you type something and click search it DOES show the correct results on the first page, but not on other pages ...

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 6:45 am
by kalpesh.mahida
because you will not have value in your $_POST["Keywords"] variable when you browse any other page of your search result.

you can do following,

use get method instead of post in your search form.
replace each occurrence of $_POST["Keywords"] with $_GET["Keywords"]
in your logic of pagination append Keywords with value of $_GET["Keywords"]

Code: Select all

$query_Recordset1 = "SELECT * FROM users WHERE users.emri LIKE '".$_POST["Keywords"]."%' OR users.rruga LIKE '".$_POST["Keywords"]."%' OR users.mbiemri LIKE '".$_POST["Keywords"]."%' OR users.link_me_kodet LIKE '".$_POST["Keywords"]."%' OR users.karta LIKE '".$_POST["Keywords"]."%' OR users.vendi LIKE '".$_POST["Keywords"]."%'";

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 7:31 am
by sHuRuLuNi
kalpesh.mahida wrote:because you will not have value in your $_POST["Keywords"] variable when you browse any other page of your search result.

you can do following,

use get method instead of post in your search form.
replace each occurrence of $_POST["Keywords"] with $_GET["Keywords"]
in your logic of pagination append Keywords with value of $_GET["Keywords"]

Code: Select all

$query_Recordset1 = "SELECT * FROM users WHERE users.emri LIKE '".$_POST["Keywords"]."%' OR users.rruga LIKE '".$_POST["Keywords"]."%' OR users.mbiemri LIKE '".$_POST["Keywords"]."%' OR users.link_me_kodet LIKE '".$_POST["Keywords"]."%' OR users.karta LIKE '".$_POST["Keywords"]."%' OR users.vendi LIKE '".$_POST["Keywords"]."%'";

Now when I type something in, it wont load the results page, because it ignores the "&mod=results" and instead it appends "imagefiled.x=10&imagefiled.y=15" - ??

But I tested it manually (i typed the missing "&mod=results" on the address bar and IT WORKS (the search results show correctly on all pages), but as I said, I have to solve this imagefield thingy now ...

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 7:34 am
by kalpesh.mahida
<input name="imageField" type="image" src="images/m12.gif">
use simple image tag instead input field.

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 7:40 am
by sHuRuLuNi
kalpesh.mahida wrote:
<input name="imageField" type="image" src="images/m12.gif">
use simple image tag instead input field.
And then how to submit the form? This image I am using is an image of a "Go!" button. I used it BECAUSE I didnt want the ugly submit buttons.

Re: Search Results lose filter after first page

Posted: Tue Jan 11, 2011 7:50 am
by sHuRuLuNi
OK I solved it by using an input "hidden" field called "mod" with the value of "results".

But why is that so? Why isnt it taking that parameter from "action"?


Anyway thank you for your help.