利用PHP訪問資料庫——實現分頁功能與多條件查詢功能

來源:互聯網
上載者:User

標籤:分頁   for   總數   hold   關鍵字   UI   多條件   and   分頁類   

1.實現分頁功能

<body>
<table width="100%" border="1">
  <thead>
    <tr>
      <th>代號</th>
      <th>名稱</th>
      <th>價格</th>
    </tr>
  </thead>
  <tbody>
      <?php
    require_once "./DBDA.class.php";  //載入資料庫訪問類的封裝檔案
    require_once "page.class.php";  //載入分頁類的封裝檔案
    $db = new DBDA();
    $sql = "select count(*) from car";
    $arr = $db->query($szts);
    
    $page = new Page($arr[0][0],5);  //查看總資料,並每頁顯示的行資料
    $sql = "select * from car".$page->limit;  //拼接每頁顯示的資料
    $arr = $db->query($sql);
    foreach($arr as $v){
        echo "<tr>
      <td>{$v[0]}</td>
      <td>{$v[1]}</td>
      <td>{$v[2]}</td>
    </tr>";
    }
    ?>  
  </tbody>
</table>

<div style="margin-top:20px">
    <?php
    echo $page->fpage();  //調用分頁類的方法
    ?>
</div>
</body>

2.實現多條件查詢功能

<body>
<?php
require_once "./DBDA.class.php";
require_once "page.class.php";
$db = new DBDA();

//做一個恒成立的條件
$tj = " 1=1 ";
$tj2 = " 1=1 ";
$name = " ";
$brand = " ";
//當提交的資料不為空白,需要改條件
if(!empty($_GET["name"])){
    $name = $_GET["name"];
    $tj = " name like ‘%{$name}%‘ ";  //使用模糊查詢查詢關鍵資訊
}
if(!empty($_GET["brand"])){
    $brand = $_GET["brand"];
    $tj2 = " brand = ‘{$brand}‘ ";
}

?>
<table width="100%" border="1">
<form action="test.php" method="get">
  <input type="text" name="name" placeholder="請輸入名稱" value="<?php echo $name ?>" style="max-width:200px; float:left">
  <input type="text" name="brand" placeholder="請輸入系列" value="<?php echo $brand ?>" style="max-width:200px; float:left">
  <button type="submit" style="float:left; margin-left:10px">查詢</button>
 </form>
  <thead>
    <tr>
      <th>代號</th>
      <th>名稱</th>
      <th>系列</th>
      <th>時間</th>
      <th>價格</th>
    </tr>
  </thead>
  <tbody>
      <?php
    $zts = "select count(*) from car where {$tj} and {$tj2}";  //多條件查詢資料總數
    $ats = $db->query($arr);
    $page = new Page($ats[0][0],2);
    
    $sql = "select * from car where {$tj} and {$tj2} ".$page->limit;
    $arr = $db->query($sql);
    foreach($arr as $v){
        //給查詢的關鍵字加字型顏色
        $n = str_replace($name,"<span style=‘color:red‘>{$name}</span>",$v[1]);       
        echo "<tr>
      <td>{$v[0]}</td>
      <td>{$n}</td>
      <td>{$v[2]}</td>
      <td>{$v[3]}</td>
      <td>{$v[7]}</td>
    </tr>";
    }
    ?>   
  </tbody>
</table>
<div>
<?php
echo $page->fpage();
?>
</div>
</body>

利用PHP訪問資料庫——實現分頁功能與多條件查詢功能

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.