加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php分页代码学习示例分享

发布时间:2020-12-12 20:04:19 所属栏目:PHP教程 来源:网络整理
导读:代码如下: 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"); //查询共有多少

代码如下:
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 "

";
echo "

php padding

";
echo "";
echo " echo "";
echo "";
echo "";
echo "

//将查询出来的数据用表格显示
$sql2 = "select * from user order by id limit {$offset},{$length}";
$ret2 = mysql_query($sql2);
while ($row2 = mysql_fetch_assoc($ret2)) {
echo " echo " echo " }

echo "

//上一页和下一页
$prevpage = $page - 1;
if ($page >= $totpage) {
$nextpage = $totpage;
} else {
$nextpage = $page + 1;
}

//跳转
echo "

上一页|下一页

";
echo "";

核心点:

<1>“$sql2 = "select * from user order by id limit {$offset},{$length}";”,$offset、$length和页数之间的关系。

<2>上一页和下一页的获得方式,以及临界点。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

IDUSERPASS