php mysql过滤结果集

时间:2015-04-07 12:53:29

标签: php mysql

我目前在一个页面上有一个搜索表单,可以在城市范围内查找各种餐馆。提交后,您将被定向到第二页,结果显示在表格中。如何实现输入文本以对字段进行搜索和过滤? (通过" city"来完善餐厅的结果。)

<body>

   <?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) <> 0  ORDER BY Name ASC, EDATE ASC LIMIT $start, $per_page") or die(mysql_error());



      echo "<table  data-toggle='table' data-sort-name='name' data-sort-order='desc' >";
      echo "<thead>";
      echo "<tr>";
      echo "<th data-sortable='true'>Date</th><th data-field='Name' data-align='left' data-sortable='true'>Name</th><th>City</th><th>Description</th>  <th>Description</th>";
      echo "</tr>";
      echo "</thead>";

      while($row = mysql_fetch_assoc($query)){
        echo "<tr>";
        echo "<td>{$row['EDATE']}</td>";
        echo "<td>{$row['Name']}</td>";
        echo "<td>{$row['PCITY']}</td>";
        echo "<td>{$row['CODE']}</td>";
        echo  '<td><a class="description" href="/results2.php?nameID=' .$row['ID'].'">' . substr($row['DESCR'], 0, 35) . '</a></td>';
        }

      echo "</table>";
      ?>

      <?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>=$pages)?$page:$page + 1;

      //if we are not on first page show first link
      if($page!=1) $pagination.= '<a class="pagination" href="?page=1"><<</a>';
      //if we are not on first page show previous link
      if($page!=1) $pagination.='<a class="pagination"  href="?page='.$Prev_Page.'&filter='.$filter.'"><</a>';


      $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>$pages)$upage=($pages-1);

      for($x=$lpage; $x<=$upage; $x++){

      $pagination.=($x == $page) ? ' <strong>'.$x.'</strong>' : ' <a class="pagination" href="?page='.$x.'&filter='.$filter.'">'.$x.'</a>' ;
      }
      //we show next link and last link if user doesn't on last page
      if($page!=$pages) $pagination.=  '  <a class="pagination" href="?page='.$Next_Page.'&filter='.$filter.'">></a>';
      if($page!=$pages) $pagination.=  ' <a class="pagination" href="?page='.$pages.'&filter='.$filter.'">>></a>';

      //display final pagination bar.
      ?>

      <div class="pagination" style="text-align:center; width: 100%;"><?php  echo $pagination; ?></div>

</div>

</body>

1 个答案:

答案 0 :(得分:0)

您可以创建一个简单的表单,让用户输入文本搜索:

...
<body>

<form type="get" name="filter_search">
  <input type="text" name="filter" /> 
  <input type="submit" value="Search" />
</form>

<?php ...