Solved

Search on PHP not Working

Posted on 2004-09-23
7
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

630 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