时间:2021-07-01 10:21:17 帮助过:21人阅读
这本质是对数据库的操作,简而言之换成SQL语句就是 select some_fields from table limit Offset Num;
这里Num是固定的,比如每页显示10条数据
关键是Offset,即偏移量,每点击下一页时候,这个值就会发生变化;如:0,10 10,10 20,10....
在页面上最终显示的只有两个东西,一个是对应的数据(图片,文字等...),一个是 分页链接(共xx条记录,每页显示10条,当前第1/32页 [首页] [上页] [下页] [尾页] )
下面具体实现代码
输出样式(可选) 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
acount(); //实例化分页类(总条数,每页显示多少条记录,执行脚本,样式) $p=new page($c,4,'page3.php',1); //获得偏移量 $of=$p->offset; //查询数据库 $res=$m->select('pic','','',"$of,4"); //便利结果集$res //输出分页链接 echo $p->style;