[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Search on PHP not Working

Posted on 2004-09-23
7
Medium Priority
?
286 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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 2000 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

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

591 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