php分页代码学习示例分享

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

//查询共有多少行数据
$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>上一页下一页的获得方式,以及临界点。

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
IDUSERPASS