1. 程式人生 > >使用PHP做分頁查詢(查詢結果也顯示為分頁)

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

val nas put post borde cnblogs cin span state

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.做分頁查詢的處理頁面

<?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>";
}

?>

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