php分頁(yè)代碼學(xué)習(xí)示例分享
<?php
header("content-type:text/html;charset=utf-8");
//數(shù)據(jù)庫(kù)連接
$conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8");
//查詢共有多少行數(shù)據(jù)
$sql1 = "select count(*) from user";
$ret1 = mysql_query($sql1);
$row1 = mysql_fetch_row($ret1);
$tot = $row1[0];
//每頁(yè)多少行數(shù)據(jù)
$length = 5;
//總頁(yè)數(shù)
$totpage = ceil($tot / $length);
//當(dāng)前頁(yè)數(shù)
$page = @$_GET['p'] ? $_GET['p'] : 1;
//limit 下限
$offset = ($page - 1) * $length;
echo "<center>";
echo "<h2>php padding</h2>";
echo "<table width='700px' border='1px' >";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>USER</th>";
echo "<th>PASS</th>";
echo "</tr>";
//將查詢出來(lái)的數(shù)據(jù)用表格顯示
$sql2 = "select * from user order by id limit {$offset}, {$length}";
$ret2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($ret2)) {
echo "<tr>";
echo "<td>{$row2['id']}</td><td>{$row2['name']}</td><td>{$row2['pass']}</td>";
echo "</tr>";
}
echo "</table>";
//上一頁(yè)和下一頁(yè)
$prevpage = $page - 1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}
//跳轉(zhuǎn)
echo "<h3><a href='index.php?p={$prevpage}'>上一頁(yè)</a>|<a href='index.php?p={$nextpage}'>下一頁(yè)</a></h3>";
echo "</center>";
核心點(diǎn):
<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和頁(yè)數(shù)之間的關(guān)系。
<2>上一頁(yè)和下一頁(yè)的獲得方式,以及臨界點(diǎn)。
相關(guān)文章
php設(shè)計(jì)模式之備忘模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之備忘模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP備忘模式相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03Zend?Framework框架實(shí)現(xiàn)發(fā)送郵件的方法
這篇文章主要介紹了Zend?Framework框架實(shí)現(xiàn)發(fā)送郵件的方法,實(shí)例分析了Zend?Framework使用smtp郵件類實(shí)現(xiàn)郵件發(fā)送的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說(shuō)明
這篇文章主要介紹了ThinkPHP5.0 圖片上傳生成縮略圖實(shí)例代碼說(shuō)明,需要的朋友可以參考下2018-06-06nginx簡(jiǎn)單配置多個(gè)php服務(wù)實(shí)例教程
nginx安裝剛安裝好是不能訪問(wèn)php文件的,需要我們進(jìn)行配置,下面這篇文章主要給大家介紹了關(guān)于nginx簡(jiǎn)單配置多個(gè)php服務(wù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01