簡體   English   中英

php mysql過濾結果集

[英]php mysql filter on results set

我目前在一頁上有一個搜索表單,可以查找城市范圍內的各種餐館。 提交后,您將直接進入第二頁,並將結果顯示在表格中。 如何實現輸入文本以對字段進行搜索和過濾? (按“城市”細化餐廳結果)。

<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>

您可以創建一個簡單的表單來讓用戶輸入文本搜索:

...
<body>

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

<?php ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM