分页程序设计理解page.class.php
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了分页程序设计理解page.class.php,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3265字,纯文字阅读大概需要5分钟。
内容图文
![分页程序设计理解page.class.php](/upload/InfoBanner/zyjiaocheng/193/7c29a30800c847f7b40e974f3f43605b.jpg)
这本质是对数据库的操作,简而言之换成SQL语句就是 select some_fields from table limit Offset Num;
这里Num是固定的,比如每页显示10条数据
关键是Offset,即偏移量,每点击下一页时候,这个值就会发生变化;如:0,10 10,10 20,10....
在页面上最终显示的只有两个东西,一个是对应的数据(图片,文字等...),一个是 分页链接(共xx条记录,每页显示10条,当前第1/32页 [首页] [上页] [下页] [尾页] )
下面具体实现代码
<?phpclass page{ //总记录数量 public $count; //每页显示多少条 public $meiye; //总页数 public $pages; //当前是第几页 public $now_page; //上一页 private $prev; //下一页 private $next; //偏移量 public $offset; //样式 public $style; //请求文件的地址 private $url; //传入4个参数:1总记录条数,2每页显示条数,3执行脚本的文件名,4输出样式(可选) public function __construct($count,$meiye,$url,$style=1){ $this->count=$count; $this->meiye=$meiye; $this->pages=ceil($this->count/$this->meiye); $this->now_page=$this->get_now_page(); $this->url=$url; $this->prev=$this->get_prev_page(); $this->next=$this->get_next_page(); $this->offset=$this->get_offset(); $this->style=$this->get_style($style); } //获得当前是第几页 private function get_now_page(){ return isset($_GET['page'])?$_GET['page']:1; } //获得上一页 private function get_prev_page(){ return $this->now_page-1?$this->now_page-1:false; } //获得下一页 private function get_next_page(){ return $this->now_page+1>$this->pages?false:$this->now_page+1; } //获得偏移量 private function get_offset(){ return $this->now_page<=1&&$this->now_page>0?0:($this->now_page-1)*($this->meiye); } //获得分页链接风格默认是: //共30条记录,每页显示4条,当前第1/8页 [首页] [下一页] [尾页] private function get_style($s){ $str=''; if ($this->pages>1) { if($s==1){ $str.="共{$this->count}条记录,每页显示{$this->meiye}条,当前第{$this->now_page}/{$this->pages}页 "; $str.="url?page=1'>[首页] "; $this->prev?$str.="url?page=$this->prev'>[上一页] ":false; $this->next?$str.="url?page=$this->next'>[下一页] ":false; $str.="url?page=$this->pages'>[尾页]"; }else{ $str.= "当前第{$this->now_page}/{$this->pages}页"; $str.="url?page=1'>[首页] "; $this->prev?$str.="url?page=$this->prev'>[上一页] ":false; for ($j = 1; $j <= $this->pages; $j++) { $str.="url?page=$j>$j "; } $this->next?$str.="url?page=$this->next'>[下一页] ":false; $str.="url?page=$this->pages'>[尾页]"; } return $str; } }}
当使用的时候,取出偏移量Offset,和style
<?php include 'mysql.class.php'; include 'page.class.php'; //实例化模型 $m=new Model('woliu'); //获得表中的所有数据 $c=$m->acount(); //实例化分页类(总条数,每页显示多少条记录,执行脚本,样式) $p=new page($c,4,'page3.php',1); //获得偏移量 $of=$p->offset; //查询数据库 $res=$m->select('pic','','',"$of,4"); //便利结果集$res //输出分页链接 echo $p->style;
内容总结
以上是互联网集市为您收集整理的分页程序设计理解page.class.php全部内容,希望文章能够帮你解决分页程序设计理解page.class.php所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。