title: php mysql filter on results set
body: <p>I currently have a search form on one page that will look up various restaurants within city limits. Once submitted, you will be directed to a second page with the results displayed within a table. How can I implement an enter text to do a search and filter upon a field? (refining the restaurant results by "city").</p>
  
  <pre><code>&lt;body&gt;
  
     &lt;?php
  
        $connection=mysql_connect('******','******','******') or die(mysql_error());
        mysql_select_db('******',$connection) or die(mysql_error());
  
        $per_page = 20;
        $adjacents = 5; 
  
        $filter = $_GET['filter'];
  
        $pages_query = mysql_query("SELECT COUNT('id') FROM broadway") or die(mysql_error());
  
       $pages = ceil(mysql_result($pages_query, 0) / $per_page);
  
        $page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1 ;
  
        $start = ($page - 1) * $per_page;
  
        $filter = mysql_real_escape_string($filter);
  
  
        $query = mysql_query("SELECT * FROM broadway WHERE TYPE = 'Food Service Establishment' AND LOCATE('".$filter."', Name) &lt;&gt; 0  ORDER BY Name ASC, EDATE ASC LIMIT $start, $per_page") or die(mysql_error());
  
  
  
        echo "&lt;table  data-toggle='table' data-sort-name='name' data-sort-order='desc' &gt;";
        echo "&lt;thead&gt;";
        echo "&lt;tr&gt;";
        echo "&lt;th data-sortable='true'&gt;Date&lt;/th&gt;&lt;th data-field='Name' data-align='left' data-sortable='true'&gt;Name&lt;/th&gt;&lt;th&gt;City&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;  &lt;th&gt;Description&lt;/th&gt;";
        echo "&lt;/tr&gt;";
        echo "&lt;/thead&gt;";
  
        while($row = mysql_fetch_assoc($query)){
          echo "&lt;tr&gt;";
          echo "&lt;td&gt;{$row['EDATE']}&lt;/td&gt;";
          echo "&lt;td&gt;{$row['Name']}&lt;/td&gt;";
          echo "&lt;td&gt;{$row['PCITY']}&lt;/td&gt;";
          echo "&lt;td&gt;{$row['CODE']}&lt;/td&gt;";
          echo  '&lt;td&gt;&lt;a class="description" href="/results2.php?nameID=' .$row['ID'].'"&gt;' . substr($row['DESCR'], 0, 35) . '&lt;/a&gt;&lt;/td&gt;';
          }
  
        echo "&lt;/table&gt;";
        ?&gt;
  
        &lt;?php 
  
        // our main pagination logic goes here
  
        //store pagination result in a string so that we can place any where in page.
        $pagination="";
        //if current page is first show first only else reduce 1 by current page
        $Prev_Page = ($page==1)?1:$page - 1;
  
        //if current page is last show last  only else add  1 to  current page
        $Next_Page = ($page&gt;=$pages)?$page:$page + 1;
  
        //if we are not on first page show first link
        if($page!=1) $pagination.= '&lt;a class="pagination" href="?page=1"&gt;&lt;&lt;&lt;/a&gt;';
        //if we are not on first page show previous link
        if($page!=1) $pagination.='&lt;a class="pagination"  href="?page='.$Prev_Page.'&amp;filter='.$filter.'"&gt;&lt;&lt;/a&gt;';
  
  
        $numberoflinks=5;
  
  
        $upage=ceil(($page)/$numberoflinks)*$numberoflinks;
  
        $lpage=floor(($page)/$numberoflinks)*$numberoflinks;
  
        $lpage=($lpage==0)?1:$lpage;
  
        $upage=($lpage==$upage)?$upage+$numberoflinks:$upage;
        if($upage&gt;$pages)$upage=($pages-1);
  
        for($x=$lpage; $x&lt;=$upage; $x++){
  
        $pagination.=($x == $page) ? ' &lt;strong&gt;'.$x.'&lt;/strong&gt;' : ' &lt;a class="pagination" href="?page='.$x.'&amp;filter='.$filter.'"&gt;'.$x.'&lt;/a&gt;' ;
        }
        //we show next link and last link if user doesn't on last page
        if($page!=$pages) $pagination.=  '  &lt;a class="pagination" href="?page='.$Next_Page.'&amp;filter='.$filter.'"&gt;&gt;&lt;/a&gt;';
        if($page!=$pages) $pagination.=  ' &lt;a class="pagination" href="?page='.$pages.'&amp;filter='.$filter.'"&gt;&gt;&gt;&lt;/a&gt;';
  
        //display final pagination bar.
        ?&gt;
  
        &lt;div class="pagination" style="text-align:center; width: 100%;"&gt;&lt;?php  echo $pagination; ?&gt;&lt;/div&gt;
  
  &lt;/div&gt;
  
  &lt;/body&gt;
  </code></pre>