分类分类
更新时间:2026-03-11 16:30:37作者:网络
文章整理了php实现分页功能的方法,提供核心思路和实现代码,希望对您有所帮助!
核心思想:
<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和页数之间的关系。
<2>上一页和下一页的获得方式,以及临界点。
<?php
/**
* php padding
*/
header("content-type:text/html;charset=utf-8");
//数据库连接
$conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());
mysql_select_db("test", $conn);
mysql_query("set names utf8");
//查询共有多少行数据
$sql1 = "select count(*) from user";
$ret1 = mysql_query($sql1);
$row1 = mysql_fetch_row($ret1);
$tot = $row1[0];
//每页多少行数据
$length = 5;
//总页数
$totpage = ceil($tot / $length);
//当前页数
$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>";
//将查询出来的数据用表格显示
$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>";
//上一页和下一页
$prevpage = $page - 1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}
//跳转
echo "<h3><a href='index.php?p={$prevpage}'>上一页</a>|<a href='index.php?p={$nextpage}'>下一页</a></h3>";
echo "</center>";
相关
Canta实用工具4.48 Mv3.2.22026-03-11
下载王者圣域策略游戏429.26 Mv2.1.0.22026-03-11
下载光强仪app实用工具4.91 Mv5.1.02026-03-11
下载光之守护者最新版角色扮演323.66 Mv1.8.92026-03-11
下载闪音跃动音乐游戏226.96 Mv1.0.12026-03-11
下载Zoom安卓版学习办公336.98 Mv6.7.5.374282026-03-11
下载奥罗拉山第二章安卓版冒险游戏864.31 Mv1.0.02026-03-11
下载遮天帝路争锋策略游戏266.95 Mv1.12026-03-11
下载黑暗信仰官方版角色扮演13.34 Mv1.0.02026-03-11
下载基金E账户app实用工具32.48 Mv2.0.322026-03-11
下载食肉牛龙模拟器中文版角色扮演155.22 Mv1.2.92026-03-11
下载联盟战歌策略游戏29.44 Mv1.02026-03-11
下载










