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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Property Animation要点总结
下一篇:springMVC 过滤/拦截器 HandlerInterceptorAdapter

发表评论

最新留言

很好
[***.229.124.182]2024年04月11日 13时54分58秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章