Search on PHP not Working

hello there,
what im trying to do now is to search on the same page key words that i type,
lets say city or zip code and to print out the tables that have the zip code or city.
please correct me this is what i have i think its the search what is wrong

<html>
<head>
<title>Welcome</title>
</head>
<body>
<FORM name="form2" action="pager.php" method="get">
<CENTER>
  <TABLE border="0" cellpadding="0" cellspacing="0">
    <TR>
      <TD></TD>
      <TD width="184" height="39" align="center">
        <TABLE border="0" cellpadding="0" cellspacing="0">
          <TR>
            <TD> <input type="text" name="q" size="20" maxlength="100" id="q" value="<?=$q?>">
            </TD>
            <TD><input type="submit" name="search" value="Search" id="search"></TD>
          </TR>
        </TABLE></TD>
    </TR>
  </TABLE>
</CENTER>
</FORM>
<?php
include("database.php");
   class Pager
   {
       function getPagerData($numHits, $limit, $page)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = (int) $page;
           $numPages = ceil($numHits / $limit);

           $page = max($page, 1);
           $page = min($page, $numPages);

           $offset = ($page - 1) * $limit;

           $ret = new stdClass;

           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;

           return $ret;
       }
}
// search start <<<<------
   $filter = "";
   if ($q)
{
   $nf = $mf = $nm = "";
   $words = explode(" ", $q);
   foreach ($words as $word)
{
   $mf .= ($mf!="" ? " OR ":"")."zip like '%$word%'";
   $nf .= ($nf!="" ? " OR ":"")."address like '%$word%'";
   $nm .= ($nm!="" ? " OR ":"")."city like '%$word%'";
}
   $filter = "WHERE $mf OR $nf OR $nm";
}
// search stop ----->>>>
// get the pager input values
    $page = $_GET['page'];
    $limit = 5;
    $result = mysql_query("select count(*) from db_street_locator");
    $total = mysql_result($result, 0, 0);

    // work out the pager values
    $pager  = Pager::getPagerData($total, $limit, $page);
    $offset = $pager->offset;
    $limit  = $pager->limit;
    $page   = $pager->page;

    // use pager values to fetch data
    $query = "select * FROM db_street_locator order by id desc limit $offset, $limit";
    $result = mysql_query($query);
// start mysql print ------->>>
      while ($row = mysql_fetch_array($result))
{
   list($id, $zip, $address, $city) = $row;
?>
<table width="45%" border="1" align="center">
  <tr>
    <td width="19%">address:</td>
      <td width="81%">
        <?=$address?>
      </td>
    </tr>
    <tr>
    <td>zip:</td>
      <td>
        <?=$zip?>
      </td>
    </tr>
    <tr>
    <td>city:</td>
      <td>
        <?=$city?>
      </td>
    </tr>
  </table>
<?php
}
// done mysql print <<<---------
?>
<table width="45%" border="1" align="center">
  <tr>
    <td><div align="center">
<?php
// start print the pager ------->>>
    // use $result here to output page content
    // output paging system (could also do it before we output the page content)
    if ($page == 1) // this is the first page - there is no previous page
        echo " ";
    else            // not the first page, link to the previous page
        echo "<a href=\"pager.php?page=" . ($page - 1) . "\">&lt;&lt;&lt; Previous</a>";
    for ($i = 1; $i <= $pager->numPages; $i++) {
      echo " ";
     if ($i == $pager->page)
          echo " $i";
      else
          echo "<a href=\"pager.php?page=$i\"> $i</a>";
}
  if ($page == $pager->numPages) // this is the last page - there is no next page
      echo " ";
  else            // not the last page, link to the next page
      echo " <a href=\"pager.php?page=" . ($page + 1) . "\"> Next &gt;&gt;&gt;</a>";
// done print the pager <<<---------
?>
</div>
</td>
  </tr>
</table>
</body>
</html>
XtryAsked:
Who is Participating?
 
ldbkuttyConnect With a Mentor Commented:
updated the above link :-)

updated script here:

<html>
<head>
<title>Welcome</title>
<script language="JavaScript">
function checkEnter(formObj,evt) {
  if (document.all) {
    if (event.keyCode==13) {
      document.form2.submit();
    }
  }
  if (document.layers) {
    if (evt.which==13) {
      document.form2.submit();
    }
  }
}
</script>
</head>
<body>
<br><br><br>

<FORM name="form2" action="pager.php" method="get">
<CENTER>
  <TABLE border="0" cellpadding="0" cellspacing="0">
    <TR>
      <TD></TD>
      <TD width="184" height="39" align="center">
        <TABLE border="0" cellpadding="0" cellspacing="0">
          <TR>
            <TD> <input type="text" name="q" size="20" maxlength="100" id="q" value="<?=(isset($_GET['q']) && !empty($_GET['q']))? $_GET['q'] : ''?>" onkeypress="checkEnter(event)">
            </TD>
            <TD><input type="submit" name="search" value="Search" id="search"></TD>
          </TR>
        </TABLE></TD>
    </TR>
  </TABLE>
</CENTER>
</FORM>
<?php
include("database.php");
   class Pager
   {
       function getPagerData($numHits, $limit, $page)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = (int) $page;
           $numPages = ceil($numHits / $limit);

           $page = max($page, 1);
           $page = min($page, $numPages);

           $offset = ($page - 1) * $limit;

           $ret = new stdClass;

           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;

           return $ret;
       }
}
   $filter = "";

   if (isset($_GET['q']) && !empty($_GET['q'])) {
       $words = explode(" ", $_GET['q']);
       foreach ($words as $word) {
           $filter .= ($filter == "" ? " WHERE " : " OR ") . "zip like '%$word%' OR address like '%$word%' OR city like '%$word%'";
       }
   }

   // get the pager input values
    $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
    $limit = 2;

    $countQuery = "select count(*) from db_street_locator $filter";
    $result = mysql_query($countQuery) or die("Count Sql Error:".mysql_error());
    $total = mysql_result($result, 0, 0);

    if($total == 0)
       die("No result found. Please try again with another search");

    // work out the pager values
    $pager  = Pager::getPagerData($total, $limit, $page);
    $offset = $pager->offset;
    $limit  = $pager->limit;
    $page   = $pager->page;

   // use pager values to fetch data
    $query = "select * FROM db_street_locator $filter order by id desc limit $offset, $limit";
    $result = mysql_query($query) or die("Select Sql Error:".mysql_error());

    while ($row = mysql_fetch_array($result)) {
       list($id, $zip, $address, $city) = $row;
?>

<table width="45%" border="1" align="center">
  <tr>
    <td width="19%">address:</td>
      <td width="81%">
        <?=$address?>
      </td>
    </tr>
    <tr>
    <td>zip:</td>
      <td>
        <?=$zip?>
      </td>
    </tr>
    <tr>
    <td>city:</td>
      <td>
        <?=$city?>
      </td>
    </tr>
  </table>  <br><br>
<?php
}
?>
<table width="45%" border="1" align="center">
  <tr>
    <td><div align="center">

<?php

    $passParameter = ( isset($_GET['q']) && !empty($_GET['q'])) ? '&q='.$_GET['q'] : '';

    // use $result here to output page content
    // output paging system (could also do it before we output the page content)
    if ($page == 1) // this is the first page - there is no previous page
        echo " ";
    else            // not the first page, link to the previous page
        echo "<a href=\"pager.php?page=" . ($page - 1) . $passParameter. "\">&lt;&lt;&lt; Previous</a>";
    for ($i = 1; $i <= $pager->numPages; $i++) {
      echo " ";
     if ($i == $pager->page)
          echo " $i";
      else
          echo "<a href=\"pager.php?page=$i\"> $i</a>";
}
  if ($page == $pager->numPages) // this is the last page - there is no next page
      echo " ";
  else            // not the last page, link to the next page
      echo " <a href=\"pager.php?page=" . ($page + 1) . $passParameter ."\"> Next &gt;&gt;&gt;</a>";

?>
      </div>
    </td>
   </tr>
</table>
</body>
</html>

I tested with limit as 2, you can change it in the program for your need. Feel free to contact me in case of doubts. :-)
0
 
AlanJDMCommented:
You are building your where clause, but then never using it. $filter contains your where clause information, but $filter is never used in a query anywhere.


Alan
0
 
XtryAuthor Commented:
ok i added $filter in

  $result = mysql_query("select count(*) from db_street_locator $filter"); and
  $query = "select * FROM db_street_locator $filter order by id desc limit $offset, $limit";
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
XtryAuthor Commented:
but same thing
0
 
ldbkuttyCommented:
Working here:  
----------------
http://jaggybala.clawz.com/xtry/pager.php

Script here:
-------------

<html>
<head>
<title>Welcome</title>
</head>
<body>
<FORM name="form2" action="pager.php" method="get">
<CENTER>
  <TABLE border="0" cellpadding="0" cellspacing="0">
    <TR>
      <TD></TD>
      <TD width="184" height="39" align="center">
        <TABLE border="0" cellpadding="0" cellspacing="0">
          <TR>
            <TD> <input type="text" name="q" size="20" maxlength="100" id="q" value="<?=(isset($_GET['q']) && !empty($_GET['q']))? $_GET['q'] : '' ?>">
            </TD>
            <TD><input type="submit" name="search" value="Search" id="search"></TD>
          </TR>
        </TABLE></TD>
    </TR>
  </TABLE>
</CENTER>
</FORM>
<?php
include("database.php");
   class Pager
   {
       function getPagerData($numHits, $limit, $page)
       {
           $numHits  = (int) $numHits;
           $limit    = max((int) $limit, 1);
           $page     = (int) $page;
           $numPages = ceil($numHits / $limit);

           $page = max($page, 1);
           $page = min($page, $numPages);

           $offset = ($page - 1) * $limit;

           $ret = new stdClass;

           $ret->offset   = $offset;
           $ret->limit    = $limit;
           $ret->numPages = $numPages;
           $ret->page     = $page;

           return $ret;
       }
}
   $filter = "";

   if (isset($_GET['q']) && isset($_GET['search']) && !empty($_GET['search'])) {
       $nf = $mf = $nm = "";
       $words = explode(" ", $_GET['q']);
       foreach ($words as $word) {
           $mf .= ($mf!="" ? " OR ":"")."zip like '%$word%'";
           $nf .= ($nf!="" ? " OR ":"")."address like '%$word%'";
           $nm .= ($nm!="" ? " OR ":"")."city like '%$word%'";
       }
       $filter = "WHERE $mf OR $nf OR $nm";
  }

   // get the pager input values
    $page = isset($_GET['page']) ? $_GET['page'] : '';
    $limit = 5;
    $result = mysql_query("select count(*) from db_street_locator $filter") or die("Count Sql Error:".mysql_error());
    $total = mysql_result($result, 0, 0);

    // work out the pager values
    $pager  = Pager::getPagerData($total, $limit, $page);
    $offset = $pager->offset;
    $limit  = $pager->limit;
    $page   = $pager->page;

    // use pager values to fetch data
    $query = "select * FROM db_street_locator $filter order by id desc limit $offset, $limit";
    $result = mysql_query($query) or die("Select Sql Error:".mysql_error());

    while ($row = mysql_fetch_array($result)) {
       list($id, $zip, $address, $city) = $row;
?>

<table width="45%" border="1" align="center">
  <tr>
    <td width="19%">address:</td>
      <td width="81%">
        <?=$address?>
      </td>
    </tr>
    <tr>
    <td>zip:</td>
      <td>
        <?=$zip?>
      </td>
    </tr>
    <tr>
    <td>city:</td>
      <td>
        <?=$city?>
      </td>
    </tr>
  </table>  <br><br>
<?php
}
?>
<table width="45%" border="1" align="center">
  <tr>
    <td><div align="center">

<?php
    // use $result here to output page content
    // output paging system (could also do it before we output the page content)
    if ($page == 1) // this is the first page - there is no previous page
        echo " ";
    else            // not the first page, link to the previous page
        echo "<a href=\"pager.php?page=" . ($page - 1) . "\">&lt;&lt;&lt; Previous</a>";
    for ($i = 1; $i <= $pager->numPages; $i++) {
      echo " ";
     if ($i == $pager->page)
          echo " $i";
      else
          echo "<a href=\"pager.php?page=$i\"> $i</a>";
}
  if ($page == $pager->numPages) // this is the last page - there is no next page
      echo " ";
  else            // not the last page, link to the next page
      echo " <a href=\"pager.php?page=" . ($page + 1) . "\"> Next &gt;&gt;&gt;</a>";

?>
      </div>
    </td>
   </tr>
</table>
</body>
</html>

Glad if it helps you. :-)
0
 
ldbkuttyCommented:
oops...i think some problem...please wait, i will have look and update again :-)
0
 
XtryAuthor Commented:
thank-you very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.