Solved

Search on PHP not Working

Posted on 2004-09-23
7
263 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now