下面小編就為大家?guī)?lái)一篇PHP實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給的大家做個(gè)參考。
分頁(yè)在后臺(tái)管理中是經(jīng)常使用的功能,分頁(yè)顯示方便大量數(shù)據(jù)的管理。
實(shí)例代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用戶列表</title>
</head>
<body>
<?php
$con = mysql_connect("localhost","root","");
mysql_query("set names utf8");
mysql_select_db("zhiye",$con);
$pageSize = 1; //每頁(yè)顯示的數(shù)量
$rowCount = 0; //要從數(shù)據(jù)庫(kù)中獲取
$pageNow = 1; //當(dāng)前顯示第幾頁(yè)
//如果有pageNow就使用,沒(méi)有就默認(rèn)第一頁(yè)。
if (!empty($_GET['pageNow'])){
$pageNow = $_GET['pageNow'];
}
$pageCount = 0; //表示共有多少頁(yè)
$sql1 = "select count(id) from user";
$res1 = mysql_query($sql1);
if($row1=mysql_fetch_row($res1)){
$rowCount = $row1[0];
}
//計(jì)算共有多少頁(yè),ceil取進(jìn)1
$pageCount = ceil(($rowCount/$pageSize));
//使用sql語(yǔ)句時(shí),注意有些變量應(yīng)取出賦值。
$pre = ($pageNow-1)*$pageSize;
$sql2 = "select * from user limit $pre,$pageSize";
$res2 = mysql_query($sql2);
while($row=mysql_fetch_assoc($res2)){
echo $row['user_name']."<br>";
echo $row['name']."<br>";
echo $row['email']."<br>";
echo $row['password']."<br>";
echo $row['tel']."<br>";
}
for ($i=1;$i<=$pageCount;$i++){
echo "<a href='userList.php?pageNow=$i'>$i</a> ";
}
?>
</body>
</html>
當(dāng)有大量數(shù)據(jù)時(shí),就不能使用上述方法。
<?php
$con = mysql_connect("localhost","root","");
mysql_query("set names utf8");
mysql_select_db("zhiye",$con);
$pageSize = 1; //每頁(yè)顯示的數(shù)量
$rowCount = 0; //要從數(shù)據(jù)庫(kù)中獲取
$pageNow = 1; //當(dāng)前顯示第幾頁(yè)
//如果有pageNow就使用,沒(méi)有就默認(rèn)第一頁(yè)。
if (!empty($_GET['pageNow'])){
$pageNow = $_GET['pageNow'];
}
$pageCount = 0; //表示共有多少頁(yè)
$sql1 = "select count(id) from user";
$res1 = mysql_query($sql1);
if($row1=mysql_fetch_row($res1)){
$rowCount = $row1[0];
}
//計(jì)算共有多少頁(yè),ceil取進(jìn)1
$pageCount = ceil(($rowCount/$pageSize));
//使用sql語(yǔ)句時(shí),注意有些變量應(yīng)取出賦值。
$pre = ($pageNow-1)*$pageSize;
$sql2 = "select * from user limit $pre,$pageSize";
$res2 = mysql_query($sql2);
//$sql = "select * from user";
//$res = mysql_query($sql,$con);
while($row=mysql_fetch_assoc($res2)){
echo $row['user_name']."<br>";
echo $row['name']."<br>";
echo $row['email']."<br>";
echo $row['password']."<br>";
echo $row['tel']."<br>";
}
if($pageNow>1){
$prePage = $pageNow-1;
echo "<a href='userList.php?pageNow=$prePage'>pre</a> ";
}
if($pageNow<$pageCount){
$nextPage = $pageNow+1;
echo "<a href='userList.php?pageNow=$nextPage'>next</a> ";
echo "當(dāng)前頁(yè){$pageNow}/共{$pageCount}頁(yè)";
}
echo "<br/><br/>";
?>
<form action="userList.php">
<input type="text" name="pageNow">
<input type="submit" value="GO">
</form>
以上這篇PHP實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示的簡(jiǎn)單實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考