利用PHP訪問數(shù)據(jù)庫_實(shí)現(xiàn)分頁功能與多條件查詢功能的示例
1.實(shí)現(xiàn)分頁功能
<body>
<table width="100%" border="1">
<thead>
<tr>
<th>代號</th>
<th>名稱</th>
<th>價格</th>
</tr>
</thead>
<tbody>
<?php
require_once "./DBDA.class.php"; //加載數(shù)據(jù)庫訪問類的封裝文件
require_once "page.class.php"; //加載分頁類的封裝文件
$db = new DBDA();
$sql = "select count(*) from car";
$arr = $db->query($szts);
$page = new Page($arr[0][0],5); //查看總數(shù)據(jù),并每頁顯示的行數(shù)據(jù)
$sql = "select * from car".$page->limit; //拼接每頁顯示的數(shù)據(jù)
$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(); //調(diào)用分頁類的方法
?>
</div>
</body>
2.實(shí)現(xiàn)多條件查詢功能
<body>
<?php
require_once "./DBDA.class.php";
require_once "page.class.php";
$db = new DBDA();
//做一個恒成立的條件
$tj = " 1=1 ";
$tj2 = " 1=1 ";
$name = " ";
$brand = " ";
//當(dāng)提交的數(shù)據(jù)不為空,需要改條件
if(!empty($_GET["name"])){
$name = $_GET["name"];
$tj = " name like '%{$name}%' "; //使用模糊查詢查詢關(guān)鍵信息
}
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}"; //多條件查詢數(shù)據(jù)總數(shù)
$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){
//給查詢的關(guān)鍵字加字體顏色
$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訪問數(shù)據(jù)庫_實(shí)現(xiàn)分頁功能與多條件查詢功能的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Laravel 5框架學(xué)習(xí)之Blade 簡介
本文給大家?guī)淼氖荓aravel5框架學(xué)習(xí)系列文章的第4篇,主要向大家簡單介紹下Blade,為什么要介紹它呢,因?yàn)閘aravel的模版引擎采用了blade模版引擎,so....2015-04-04
基于thinkphp5框架實(shí)現(xiàn)微信小程序支付 退款 訂單查詢 退款查詢操作
這篇文章主要介紹了基于thinkphp5框架實(shí)現(xiàn)微信小程序支付 退款 訂單查詢 退款查詢操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
WordPress偽靜態(tài)規(guī)則設(shè)置代碼實(shí)例
這篇文章主要介紹了WordPress偽靜態(tài)規(guī)則設(shè)置代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
php實(shí)現(xiàn)爬取和分析知乎用戶數(shù)據(jù)
本文給大家介紹的是利用php的curl編寫的爬取知乎用戶數(shù)據(jù)的爬蟲,并分析用戶的各種屬性,有需要的小伙伴可以參考下2016-01-01
Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié)
這篇文章主要介紹了Yii2框架數(shù)據(jù)庫簡單的增刪改查語法小結(jié),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-08-08
Linux中用PHP判斷程序運(yùn)行狀態(tài)的2個方法
這篇文章主要介紹了Linux中用PHP判斷程序運(yùn)行狀態(tài)的2個方法,需要的朋友可以參考下2014-05-05
destoon調(diào)用discuz論壇中帶圖片帖子的實(shí)現(xiàn)方法
這篇文章主要介紹了destoon調(diào)用discuz論壇中帶圖片帖子的實(shí)現(xiàn)方法,是destoon開發(fā)中非常實(shí)用的一個技巧,需要的朋友可以參考下2014-08-08

