PHP做分頁查詢(查詢結果也顯示為分頁)


1.先把數據庫里所有的數據分頁顯示在頁面,並在顯示數據的表格上方加上查詢表單。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<br />
<form action="fenyechaxun.php" method="get"><!--分頁查詢最好使用get,不要使用post,使用post會變得很復雜。-->
    <div>關鍵字:<input type="text" name="key" />
        <input type="submit" value="查詢" />
    </div>
</form>
<br />
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <th>地區代號</th><!--坐標頭-->
    <th>地區名稱</th>
    <th>上級代號</th>
</tr>
<?php
include("DBDA.class.php");//引入封裝類的頁面
$db = new DBDA();//造一個對象

//求數據的總條數
$sall = "select count(*) from chinastates";
$total = $db->StrQuery($sall);

include("page.class.php");//引入分頁類的頁面
$page = new Page($total,20);//造一個分頁的對象.第一個參數是數據的總條數,第二個參數是每頁顯示多少條數據。

$sql = "select * from chinastates ".$page->limit;//調用分頁里面的limit方法。
$attr = $db->Query($sql);

foreach($attr as $v)
{
    echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td></tr>";
}

?>
</table>
<?php
echo $page->fpage();//顯示表格下方的數據和頁面的信息。
?>
</body>
</html>

 

2.加上條件,實現目標結果。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<br />
<form action="fenyechaxun.php" method="get">
    <div>關鍵字:<input type="text" name="key" />
        <input type="submit" value="查詢" />
    </div>
</form>
<br />
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
    <th>地區代號</th>
    <th>地區名稱</th>
    <th>上級代號</th>
</tr>
<?php
include("DBDA.class.php");
$db = new DBDA();

//查詢條件
$tj1 = " 1=1 ";
if(!empty($_GET["key"]))//獲取提交的關鍵字
{
    $tj1 = " areaname like '%{$_GET['key']}%'";
}


$sall = "select count(*) from chinastates where {$tj1}";//把條件拼接到語句中
$total = $db->StrQuery($sall);

include("page.class.php");
$page = new Page($total,20);

$sql = "select * from chinastates where {$tj1} ".$page->limit;//這里也要加上搜索條件
$attr = $db->Query($sql);

foreach($attr as $v)
{
    echo "<tr><td>{$v[0]}</td><td>{$v[1]}</td><td>{$v[2]}</td></tr>";
}

?>

 

</table>
<?php
echo $page->fpage();
?>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM