Solved

Search on PHP not Working

Posted on 2004-09-23
7
273 Views
Last Modified: 2013-12-12
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>
0
Comment
Question by:Xtry
  • 3
  • 3
7 Comments
 
LVL 9

Expert Comment

by:AlanJDM
ID: 12134347
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
 

Author Comment

by:Xtry
ID: 12135041
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
 

Author Comment

by:Xtry
ID: 12135585
but same thing
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 32

Expert Comment

by:ldbkutty
ID: 12138265
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
 
LVL 32

Expert Comment

by:ldbkutty
ID: 12138795
oops...i think some problem...please wait, i will have look and update again :-)
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 500 total points
ID: 12139218
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
 

Author Comment

by:Xtry
ID: 12142487
thank-you very much!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question