好贷网好贷款

PHP分页类

发布时间:2016-12-5 20:30:26 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"PHP分页类",主要涉及到PHP分页类方面的内容,对于PHP分页类感兴趣的同学可以参考一下。

class page{ private $nowPage;//当前页 private $pageSize;//每页记录数 private $navLen;//数字导航条的长度 private $totalRs;//总记录数 public $totalPages;//总页数 public $sql;//SQL语句变量 private $target;//打开链接方式,默认为_self public $html=''; private $link;//数据库链接标识 /** *$table 数据表的名称 *$link 数据库链接 *$pageSize 每页记录数,默认为20 *$navLen 数字导航条的长度,默认为7 */ public function __construct($table,$link,$pageSize=20,$navLen=7,$target=0){ $this->html = ''; $this->pageSize = $pageSize; $this->navLen = $navLen; $this->link = $link; switch($target){ case 0: $this->target = '_self';break; case 1: $this->target = '_blank';break; } //获取总记录数 $this->sql = 'SELECT * FROM '.$table; $result = @mysql_query($this->sql,$this->link); $this->totalRs = mysql_num_rows($result); //获取总页数 $this->totalPages = ceil($this->totalRs/$pageSize); //获取当前页 $this->nowPage = isset($_GET['p']) ? $_GET['p'] : 1; //生成导航条代码 $this->html(); //生成分页记录代码 $this->handle(); } private function handle(){ $this->sql = $this->sql.' LIMIT '.($this->nowPage-1)*$this->pageSize.','.$this->pageSize.''; } private function get_first_page(){ if($this->nowPage <= 1){ return '<a href="javascript:void(0)">首页</a>'; }else{ return '<a href="?p=1">首页</a>'; } } private function get_prev_page(){ if($this->nowPage <= 1){ return '<a href="javascript:void(0)">上一页</a>'; }else{ return '<a href="?p='.($this->nowPage-1).'">上一页</a>'; } } public function get_num_nav(){ $str = ''; $a = intval(($this->nowPage-1)/$this->navLen); for($i=1;$i<=$this->navLen;$i++){ $c = $a*$this->navLen+$i; if($c<=$this->totalPages){ if($c==$this->nowPage){ $str .= '<a href="javascript:void(0)" target="'.$this->target.'">'.$c.'</a>'; }else{ $str .= '<a href="?p='.$c.'" target="'.$this->target.'">'.$c.'</a>'; } } } return $str; } private function get_next_page(){ if($this->nowPage >= $this->totalPages){ return '<a href="javascript:void(0)">下一页</a>'; }else{ return '<a href="?p='.($this->nowPage+1).'">下一页</a>'; } } private function get_last_page(){ if($this->nowPage >= $this->totalPages){ return '<a href="javascript:void(0)">尾页</a>'; }else{ return '<a href="?p='.$this->totalPages.'">尾页</a>'; } } private function html(){ $this->html = $this->get_first_page(); $this->html .= $this->get_prev_page(); $this->html .= $this->get_num_nav(); $this->html .= $this->get_next_page(); $this->html .= $this->get_last_page(); } }

上一篇:JAVA_WEB Struts2学习:Struts2解耦、耦合方式获取request、session、aplication对象
下一篇:hashcode

相关文章

关键词: PHP分页类

相关评论