php mysql分页操作
发布日期:2021-11-21 04:40:49
浏览次数:14
分类:技术文章
本文共 2019 字,大约阅读时间需要 6 分钟。
首先建立NewsModle类用于操作数据库
在该类中有两个操作第一个获取 总记录数
//返回所有记录数目 public function getCount($tableName) { $sql = "select count(*) from {$tableName}"; $result=$this->mysqli->query($sql); $row = $result->fetch_assoc(); return $row["count(*)"]; }另外一个是得到分页数据
//得到分页 public function selectLimitPage($pager) { $tableName = $pager->tablename; $offset = (($pager->CurrentPageID)-1)*($pager->PageSize); $pageSize = $pager->PageSize; $sql = "SELECT * FROM {$tableName} LIMIT {$offset},{$pageSize}"; if($result=$this->mysqli->query($sql)) { if($result->num_rows) { while($row=$result->fetch_assoc()) $allNews[] = new NewsToUser($row["title"],$row["decription"],$row["pic_url"],$row["artcile_url"]); $result->close(); return $allNews; } else{ $result->close(); $this->printError("没有获取到任何记录"); return FALSE; } } else{ $this->printError("数据查询失败:".$this->mysqli->error); return FALSE; } }然后建立一个用于操作分页数据的page类
tablename = $tablename; //每个页面的 $this->PageSize = $PageSize; $this->db = new NewsModle(); //得到条目数目 $this->numItems = $this->db->getCount($tablename); $this->numPages = ceil($this->numItems/$PageSize); echo $this->numPages; } public function showInfo() { if(isset($_GET["CurrentPageID"])) { $this->CurrentPageID = $_GET["CurrentPageID"]; } if($this->CurrentPageID<=1) { echo "第{$this->CurrentPageID}页 下一页"; } else if($this->CurrentPageID>=$this->numPages) { echo "上一页 第{$this->CurrentPageID}页"; } else { echo "上一页 第{$this->CurrentPageID}页 下一页"; } } public function pre() { if($this->CurrentPageID <= 1) { return 1; } else { return $this->CurrentPageID-1; } } public function nex() { if($this->CurrentPageID >= $this->numPages ) { return $this->CurrentPageID; } else { return $this->CurrentPageID +1; } } public function showData() { //得到数据 print_r($this->db->selectLimitPage($this) ); } /** * @return the $CurrentPageID */ public function getCurrentPageID() { return $this->CurrentPageID; }}?>运行结果如下:
转载地址:https://blog.csdn.net/xiaoji94shen/article/details/17586791 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
很好
[***.229.124.182]2024年04月11日 13时54分58秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unity使用UnityWebRequest实现本地日志上传到web服务器
2019-04-27
Unity使用RenderTexture实现裁切3D模型
2019-04-27
美术和程序吵架,原来是资源序列化格式设置不统一
2019-04-27
Unity iOS接SDK,定制UnityAppController
2019-04-27
Unity iOS接SDK前先要了解的知识(Objective-C)
2019-04-27
记一次iOS闪退问题的定位:NSLog闪退
2019-04-27
Unity打开照相机与打开本地相册然后在Unity中显示照片(Android与iOS)
2019-04-27
无需接入SDK即可在Unity中获取经纬度(Android/iOS),告诉我你的坐标
2019-04-27
Unity获取系统信息SystemInfo(CPU、显卡、操作系统等信息)
2019-04-27
Unity中获取物体的尺寸(size)的三种方法
2019-04-27
Unity中的关节组件和绳子效果的实现
2019-04-27
Unity可视化编程插件: Bolt,可以像UE4的蓝图那样啦
2019-04-27
Android的.dex、.odex与.oat文件扫盲
2019-04-27
Unity移动应用如何在Bugly上查看崩溃堆栈
2019-04-27
unity3D 在屏幕边框创建碰撞框
2019-04-27
xml中常用的转义符
2019-04-27
关于MSDK的几个难点
2019-04-27
使用UnityEditor做工具
2019-04-27