php实现分页效果
废话不多说,先上代码:
<?php //分页的函数 function news($pageNum = 1, $pageSize = 3) { $array = array(); $conn = mysqli_connect("localhost", "root", "022414ls", "web08",‘3308‘) or die("连接数据库服务器失败!".mysqli_connect_error()); //连接MySQL服务器,选择数据库 mysqli_query($conn,"set names utf8"); // limit为约束显示多少条信息,后面有两个参数,第一个为从第几个开始,第二个为长度 $rs = "select * from product limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize; $r = mysqli_query($conn, $rs); while ($obj = mysqli_fetch_object($r)) { $array[] = $obj; } mysqli_close($conn); return $array; } //显示总页数的函数 function allNews() { $conn = mysqli_connect("localhost", "root", "022414ls", "web08",‘3308‘) or die("连接数据库服务器失败!".mysqli_connect_error()); //连接MySQL服务器,选择数据库 mysqli_query($conn,"set names utf8"); $rs = "select count(*) num from product"; //可以显示出总页数 $r = mysqli_query($conn, $rs); $obj = mysqli_fetch_object($r); mysqli_close($conn); return $obj->num; } @$allNum = allNews(); @$pageSize = 3; //约定没页显示几条信息 @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"]; @$endPage = ceil($allNum/$pageSize); //总页数 @$array = news($pageNum,$pageSize); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title><link href="mycss.css" rel="stylesheet" type="text/css"> <title>115酒馆管理员界面</title> </head> <style type="text/css"> </style> <div class="top"> <center> <ul> <li><a href="admin_main.php">管理员首页</a></li> <li><a href="admin_add.html">新增商品</a></li> <li><a href="admin_form.html">管理订单</a></li> </ul> </center> </div> <div align="center"> <table width="1200" border="1" align="center"> <tr> <th>饮品id</th> <th>饮品名称</th> <th>饮品品牌</th> <th>饮品种类</th> <th>饮品价格</th> <th>饮品图片</th> <th>操作</th> </tr> <?php include_once "test_conn.php"; $sql ="select * from product"; $result= fun_conn($sql); // while($row = mysqli_fetch_array($result)) { // echo "<tr>"; // echo "<td>{$row[‘pid‘]}</td>"; // echo "<td>{$row[‘pname‘]}</td>"; // echo "<td>{$row[‘pbrand‘]}</td>"; // echo "<td>{$row[‘ptype‘]}</td>"; // echo "<td>{$row[‘pprice‘]}</td>"; // echo "<td align=center><img src=\"".$row[‘purl‘]."\" width=100 height=100/></td>"; foreach($array as $key=>$values){ echo "<tr>"; echo "<td>{$values->pid}</td>"; echo "<td>{$values->pname}</td>"; echo "<td>{$values->pbrand}</td>"; echo "<td>{$values->ptype}</td>"; echo "<td>{$values->pprice}</td>"; echo "<td align=center><img src=\"".$values->purl."\" width=100 height=100/></td>"; echo "<td> <a href=‘admin_upd.php?id={$values->pid}‘>修改</a> <a href=‘javascript:del({$values->pid})‘>删除</a> </td>"; echo "</tr>"; } ?> </table> <div> <a href="?pageNum=1">首页</a> <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>">上一页</a> <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>">下一页</a> <a href="?pageNum=<?php echo $endPage?>">尾页</a> </div> </div> <a href="admin_add.html">增加商品</a> <script type="text/javascript"> function del(id) { if(confirm("确定删除??")){ window.location ="admin_del.php?pid="+id; } } </script> </body> </html>
其中有一个表,名为product
相关的建表语句,和插入案例如下:
DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `pid` int(11) NOT NULL AUTO_INCREMENT, `pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `pbrand` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `ptype` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `pprice` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `purl` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, PRIMARY KEY (`pid`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of product -- ---------------------------- INSERT INTO `product` VALUES (1, ‘泸州老窖‘, ‘泸州老窖‘, ‘白酒‘, ‘289‘, ‘image/lzlj.jpg‘); INSERT INTO `product` VALUES (2, ‘江小白300ML‘, ‘江小白‘, ‘白酒‘, ‘28‘, ‘image/jxb.jpg‘); INSERT INTO `product` VALUES (3, ‘贵州茅台600ML‘, ‘茅台酒业‘, ‘白酒‘, ‘128‘, ‘image/bj.jpg‘); INSERT INTO `product` VALUES (4, ‘赤霞珠干红‘, ‘奔富酒庄‘, ‘白酒‘, ‘388‘, ‘image/cxgh.jpg‘); INSERT INTO `product` VALUES (5, ‘可口可乐‘, ‘可口可乐‘, ‘汽水‘, ‘2.5‘, ‘image/kl.jpg‘); INSERT INTO `product` VALUES (6, ‘雪碧‘, ‘可口可乐‘, ‘汽水‘, ‘2.5‘, ‘image/xb.jpg‘); INSERT INTO `product` VALUES (7, ‘冰绿茶‘, ‘统一‘, ‘茶饮‘, ‘3‘, ‘image/lc.jpg‘); INSERT INTO `product` VALUES (8, ‘冰红茶‘, ‘统一‘, ‘茶饮‘, ‘3‘, ‘image/hc.jpg‘);
在image文件(项目中的image)下有几张图片,可以自己随便找点就行
下面说一下具体的步骤:
1.建立一个函数,用来实现限制查询的功能
首先我们来想一下,实现这个功能我们首先要知道当前的页数和要限制的一个页面显示几条信息,这个可以用形参传过来,那么,
我们设置pageNum为页数,pageSize为一个页面显示几条数据,在 "select * from tableName limit num1,num2”这个查询语句中,
num1就是(pageNum-1)*pageSize,num2就是pageSize,明白了这个关系之后,那么就好办了,代码在下面
//分页的函数 function news($pageNum = 1, $pageSize = 3) { $array = array(); $coon = mysqli_connect("localhost", "root"); mysqli_select_db($coon, "jereh"); mysqli_set_charset($coon, "utf8"); // limit为约束显示多少条信息,后面有两个参数,第一个为从第几个开始,第二个为长度 $rs = "select * from n_content limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize; $r = mysqli_query($coon, $rs); while ($obj = mysqli_fetch_object($r)) { $array[] = $obj; } mysqli_close($coon,"jereh"); return $array; }
上面的代码中,值得注意的是,limit后面一定要加一个空格,不然会出错误;
2.建立第二个函数,用来显示总页数的函数
这个没什么新的知识点,都是以前写过的知识点了,直接看代码吧
//显示总页数的函数 function allNews() { $coon = mysqli_connect("localhost", "root"); mysqli_select_db($coon, "jereh"); mysqli_set_charset($coon, "utf8"); $rs = "select count(*) num from n_content"; //可以显示出总页数 $r = mysqli_query($coon, $rs); $obj = mysqli_fetch_object($r); mysqli_close($coon,"jereh"); return $obj->num; }
3.调用这两个函数,进行初步的处理
我们想做下一页下一页的结果,需要用到get方法在本页面传数据,那么我们在刷新页面的时候,肯定get是得不到数据的,所以需要用三目运算了,看代码了
<?php @$allNum = allNews(); @$pageSize = 3; //约定每页显示的信息条数 @$pageNum = empty($_GET["pageNum"])?1:$_GET["pageNum"]; @$endPage = ceil($allNum/$pageSize); //总页数 @$array = news($pageNum,$pageSize); ?>
4.在页面进行显示
在页面显示的话,需要用到了foreach循环了,代码简单,看下面
<table border="1" style="text-align: center" cellpadding="0"> <tr> <td>编号</td> <td>新闻标题</td> <td>来源</td> <td>点击率</td> <td>发布日期</td> </tr> <?php foreach($array as $key=>$values){ echo "<tr>"; echo "<td>{$values->id}</td>"; echo "<td>{$values->title}</td>"; echo "<td>{$values->src}</td>"; echo "<td>{$values->indexs}</td>"; echo "<td>{$values->times}</td>"; echo "</tr>"; } ?> </table>
5.实现上一页,下一页的效果
要实现页面跳转的效果,我们需要用到了a标签的href属性,地址写“?pageNum = ...”这个pageNum是我们自己定义的,由于是get来传递的,在上一步里面我们已经用$_GET接受了参数,所以我们只需要进行get的参数传递就可以了;
首页:“pageNum=1”;
上一页:"pageNum=<?php echo $pageNum==1?1:$pageNum-1?>"
下一页:"pageNum=<?php echo $pageNum==$endPage ?$endPage :$pageNum+1?>"
尾页:“pageNum=<?php echo $pageNum =$endPage?>”;
特别注意的是,”pageNum=”的时候后面千万千万不要空格,代码如下
<a href="?pageNum=1">首页</a> <a href="?pageNum=<?php echo $pageNum==1?1:($pageNum-1)?>">上一页</a> <a href="?pageNum=<?php echo $pageNum==$endPage?$endPage:($pageNum+1)?>">下一页</a> <a href="?pageNum=<?php echo $endPage?>">尾页</a>