php中分页原与与分页类
发布:smiling 来源: PHP粉丝网 添加日期:2014-03-15 13:29:10 浏览: 评论:0
所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数: 每页多少条记录($PageSize)?
当前是第几页($CurrentPageID)?
现在只要再给我一个结果集,我就可以显示某段特定的结果出来.
至于其他的参数,比如:上一页($PReviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到.
以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows.看看下面一组sql语句,尝试一下发现其中的规率.
前10条记录:select * from table limit 0,10
第11至20条记录:select * from table limit 10,10
第21至30条记录:select * from table limit 20,10
这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:
select * from table limit ($CurrentPageID – 1) * $PageSize, $PageSize
拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事.搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了.以下我将用具体代码加以说明.
3、简单代码
请详细阅读以下代码,自己调试运行一次,最好把它修改一次,加上自己的功能,比如搜索等等.
实例代码如下:
- <?php
- // 建立数据库连接
- $link = mysql_connect(“localhost”, “mysql_user”, “mysql_passWord”)
- or die(“Could not connect: ” . mysql_error());
- // 获取当前页数
- if( isset($_GET['page']) ){
- $page = intval( $_GET['page'] );
- }
- else{
- $page = 1;
- }
- // 每页数量
- $PageSize = 10;
- // 获取总数据量
- $sql = “select count(*) as amount from table”;
- $result = mysql_query($sql);
- $row = mysql_fetch_row($result);
- $amount = $row['amount'];
- // 记算总共有多少页
- if( $amount ){
- if( $amount < $page_size ){ $page_count = 1; } //如果总数据量小于$PageSize,那么只有一页
- if( $amount % $page_size ){ //取总数据量除以每页数的余数
- $page_count = (int)($amount / $page_size) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一
- }else{
- $page_count = $amount / $page_size; //如果没有余数,则页数等于总数据量除以每页数的结果
- }
- }
- else{
- $page_count = 0;
- }
- // 翻页链接
- $page_string = ”;
- if( $page == 1 ){
- $page_string .= ‘第一页|上一页|’;
- }
- else{
- $page_string .= ‘<a href=?page=1>第一页</a>|<a href=?page=’.($page-1).’>上一页</a>|’;
- }
- if( ($page == $page_count) || ($page_count == 0) ){
- $page_string .= ‘下一页|尾页’;
- }
- else{
- $page_string .= ‘<a href=?page=’.($page+1).’>下一页</a>|<a href=?page=’.$page_count.’>尾页</a>’;
- }
- // 获取数据,以二维数组格式返回结果
- if( $amount ){
- $sql = “select * from table order by id desc limit “. ($page-1)*$page_size .”, $page_size”;
- $result = mysql_query($sql);
- while ( $row = mysql_fetch_row($result) ){
- $rowset[] = $row;
- }
- }else{
- $rowset = array();
- }
- // 没有包含显示结果的代码,那不在讨论范围,只要用foreach就可以很简单的用得到的二维数组来显示结果
- ?>
PHP分页器制作,自动生成分面页码,JS调用函数
实例代码如下:
- <?php
- class PageView{
- /**页码**/
- public $pageNo = 1;
- /**页大小**/
- public $pageSize = 20;
- /**共多少页**/
- public $pageCount = 0;
- /**总记录数**/
- public $totalNum = 0;
- /**偏移量,当前页起始行**/
- public $offSet = 0;
- /**每页数据**/
- public $pageData = array();
- /**是否有上一页**/
- public $hasPrePage = true;
- /**是否有下一页**/
- public $hasNextPage = true;
- public $pageNoList = array();
- public $jsFunction ='jsFunction';
- /**
- *
- * @param unknown_type $count 总行数
- * @param unknown_type $size 分页大小
- * @param unknown_type $string
- */
- public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){
- $this->totalNum = $count;//总记录数
- $this->pageSize = $size;//每页大小
- $this->pageNo = $pageNo;
- //计算总页数
- $this->pageCount = ceil($this->totalNum/$this->pageSize);
- $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount;
- //检查pageNo
- $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo;
- $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo;
- //计算偏移
- $this->offset = ( $this->pageNo - 1 ) * $this->pageSize;
- //计算是否有上一页下一页
- $this->hasPrePage = $this->pageNo == 1 ?false:true;
- $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true;
- $this->pageData = $pageData;
- $this->jsFunction = $jsFunction;
- }
- /**
- * 分页算法
- * @return
- */
- private function generatePageList(){
- $pageList = array();
- if($this->pageCount <= 9){
- for($i=0;$i<$this->pageCount;$i++){
- array_push($pageList,$i+1);
- }
- }else{
- if($this->pageNo <= 4){
- for($i=0;$i<5;$i++){
- array_push($pageList,$i+1);
- }
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
- }else if($this->pageNo > $this->pageCount - 4){
- array_push($pageList,1);
- array_push($pageList,-1);
- for($i=5;$i>0;$i--){
- array_push($pageList,$this->pageCount - $i+1);
- }
- }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){
- array_push($pageList,1);
- array_push($pageList,-1);
- array_push($pageList,$this->pageNo -2);
- array_push($pageList,$this->pageNo -1);
- array_push($pageList,$this->pageNo);
- array_push($pageList,$this->pageNo + 1);
- array_push($pageList,$this->pageNo + 2);
- array_push($pageList,-1);
- array_push($pageList,$this->pageCount);
- }
- }
- return $pageList;
- }
- /***
- * 创建分页控件
- * @param
- * @return String
- */
- public function echoPageAsDiv(){
- $pageList = $this->generatePageList();
- $pageString ="<div class='pagination'><div class='page-bottom'>";
- if(!emptyempty($pageList)){
- if($this->pageCount >1){
- if($this->hasPrePage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一页</a>";
- }
- foreach ($pageList as $k=>$p){
- if($this->pageNo == $p){
- $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>";
- continue;
- }
- if($p == -1){
- $pageString = $pageString ."<span class='page-break'>...</span>";
- continue;
- }
- $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>";
- }
- if($this->hasNextPage){
- $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一页</a>";
- }
- }
- }
- $pageString = $pageString .("</div></div>");
- return $pageString;
- }
- }
- ?>
- <style type="text/css">
- <!--
- .pagination {font-family: Tahoma;overflow: hidden; padding-top: 12px; text-align: center;}
- .pagination-tab { margin-bottom: 20px;}
- .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip {
- display: inline-block;font-family: Tahoma,SimSun,Arial; height: 22px;line-height:22px; margin: 0; min-width: 16px;padding: 0 5px; text-align: center; vertical-align: top; white-space: nowrap;}
- .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break {
- border: 1px solid #ed3d83; color:#e9357d; font-weight:bold;}
- .pagination a:hover { border: 1px solid #ed3d83;text-decoration: none; background-color:#f95f9d; color:#fff;}
- .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width: 36px; background-image: url(../static/img/page.gif);}
- .pagination .page-prev { background-position: -0px -38px; padding-left: 16px;}
- .pagination .page-prev_g { background-position:0px -59px; padding-left: 16px; color:#cbcbcb; font-weight:normal;}
- .pagination .page-next { background-position: 0px 0px; padding-right: 16px; font-weight:normal;}
- .pagination .page-next_g { background-position: -0px -19px; padding-right: 16px; color:#cbcbcb;}
- .pagination .page-cur {background-color: #f95f9d; border: 1px solid #ed3d83;color: #fff;font-weight: bold;}
- .pagination .page-break {border: medium none; background:none transparent; color:#333;}
- -->
- </style>
PHP调用实例代码如下:
- $pageNo = $_GET['pageNo'];
- if(emptyempty($pageNo)){
- }
- //分页数据
- $pageData = News::getNewsPage($pageNo,$pageSize);
- //取得总行数
- $count = News::getNewsCount();
- //创建分页器
- $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData);
- //生成页码
- $pageViewString = $p->echoPageAsDiv();
Tags: $PageSize $CurrentPageID
- 上一篇:一个简单php和mysql数据分页程序
- 下一篇:简单入门级php分页代码
相关文章
- ·php分页代码与分页原理(2013-11-12)
- ·PHP分页显示制作(2013-11-13)
- ·PHP实现翻页跳转功能(2013-11-13)
- ·PHP分页显示制作详细讲解(2013-11-13)
- ·自定义PHP分页函数(2013-11-13)
- ·小谈PHP&MYSQL分页原理及实现(2013-11-13)
- ·简单的分页函数(2013-11-28)
- ·php中分页显示文章标题(2013-12-10)
- ·PHP MySQL分页显示(2013-12-10)
- ·一款php分页代码(2013-12-16)
- ·php分页代码(2013-12-16)
- ·一个实例php mysql模板分页类(2013-12-19)
- ·理解php分页思路方法详解(2014-01-03)
- ·PHP+AJAX无刷新分页实现代码详解(2014-01-03)
- ·php长文章分页原理与实现程序(2014-01-05)
- ·PHP 分页代码与分页原理解析(2014-01-06)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)