php通用分页类代码 |
时间:2015-01-23 来源:西部数据 作者:西部数据 |
|
- class dividepage{
- private $total;
- private $url;
- private $displaypg;
- private $page;
- private $lastpg;
- private $prepg;
- private $nextpg;
- private $firstcount;
- private $startd;
- private $stopd;
-
- public function __construct($url, $total, $displaypg){
- $this->url = $url;
- $this->total = $total;
-
- $this->displaypg = $displaypg;
- $this->initdividepage();
-
- }
-
- private function initdividepage(){
-
- $parse_url = parse_url($this->url);
- $url_query = $parse_url['query'];
- if($url_query){
- ereg('(^|&)page=([0-9]*)', $url_query, $k);
- $this->page = $k[2];
- $url_query = ereg_replace("(^|&)page=$this->page", '', $url_query);
- $this->url = str_replace($parse_url['query'], $url_query, $this->url);
- $this->page = $this->page ? $this->page : 1;
- if($url_query){
- $this->url .= '&page';
- }else{
- $this->url .= 'page';
- }
- }else{
- $this->page = 1;
- $this->url .= '?page';
- }
- $this->lastpg = ceil($this->total / $this->displaypg);
- $this->page = min($this->lastpg, $this->page);
- $this->prepg = $this->page - 1;
- $this->nextpg = $this->page + 1;
- $this->firstcount = ($this->page - 1) * $this->displaypg;
- $this->startd = $this->total ? ($this->firstcount + 1) : 0;
- $this->stopd = min($this->firstcount + $this->displaypg, $this->total);
-
-
- }
- public function getpageinfo(){
- return '<span class="pageinfostyle">显示第<span class="numstyle">'.$this->startd.'-'.$this->stopd.'</span>条记录,共<span class="numstyle">'.$this->total.'</span>条记录。</span>';
- }
- public function getcommonpagenav(){
- $commonnav = '';
- if($this->lastpg == 1){
- return $commonnav;
- break;
- }
- $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首页</a>';
- if($this->prepg){
- $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一页</a>';
- }else{
- $commonnav .= '<a class="fcompagestyle">上一页</a>';
- }
- if($this->nextpg <= $this->lastpg){
- $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一页</a>';
- }else{
- $commonnav .= '<a class="fcompagestyle">下一页</a>';
- }
- $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾页</a>';
- return $commonnav;
- }
-
- public function getjumppagenav(){
-
- $jumpnav = '<span class="pageinfostyle">到第<select name="topage" size="1" class="topage" onchange='window.location="'.$this->url.'="+this.value'>'." ";
- for($i = 1; $i <= $this->lastpg; $i++){
- if($i == $this->page){
- $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." ";
- }else{
- $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." ";
- }
- }
- $jumpnav .= '</select>页,共<span class="numstyle">'.$this->lastpg.'</span>页</span>';
- return $jumpnav;
- }
-
- public function getallpagenav(){
- $temp = $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav();
- return $temp;
- }
-
- public function getlimitstr(){
-
-
-
-
- $temp = $this->firstcount.','.$this->displaypg;
-
- return $temp;
- }
- }
使用实例,代码如下:
- $result=mysql_query("select * from tb_pagetest");
- $total=mysql_num_rows($result);
- $pagesize = 5;
- $url = $_server['request_uri'];
-
- $dividepageclass = new dividepage($url, $total, $pagesize);
- $limitstr = $dividepageclass->getlimitstr();
- echo $dividepageclass->getallpagenav();
- 如:显示第11-13条记录,共13条记录。首页 上一页 下一页 尾页 到*第 1 页,共 3 页
- $sql = 'select * from tb_pagetest limit '.$limitstr;
- $result=mysql_query($sql);
- 如:
- while($row=mysql_fetch_array($result))
- echo "<hr><b>".$row[title]." | ".$row[author];
|
|
|
|