PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php通用分页类代码带实例教程

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:classdividepage{//分页类private$total;//要显示的总记录数private$url;//请求的url地址private$displaypg;//每页显示的记录数,默认为每...
    1. class dividepage{//分页类
    2.  private $total;//要显示的总记录数
    3.  private $url;//请求的url地址
    4.  private $displaypg;//每页显示的记录数,默认为每页显示10条记录
    5.  private $page;//当前页码
    6.  private $lastpg;//总页数,即最后一页的页码
    7.  private $prepg;//前一页
    8.  private $nextpg;//后一页
    9.  private $firstcount;//记录条数开始的序号从0开始
    10.  private $startd;//记录条数开始的记录号.
    11.  private $stopd;//记录条数结束的记录号.
    12. //构造函数
    13. public function __construct($url$total$displaypg){ 
    14.  $this->url = $url;//请求的url
    15.  $this->total = $total;//总记录数
    16.  //if($displaypg == '')
    17.  $this->displaypg = $displaypg;//每页显示的记录数
    18.  $this->initdividepage();//初始化分页类
    19.  //echo ','.$this->displaypg;
    20. //初始化分页类
    21. private function initdividepage(){ 
    22.  //分析url
    23.  $parse_url = parse_url($this->url);//将url解释为有固定键值对的数组
    24.  $url_query = $parse_url['query'];//取出url中的查询字符串
    25.  if($url_query){//如果有查询字符串,则删除查询字串中当前页的查询字段如:&page=$page或page=$page
    26.   ereg('(^|&)page=([0-9]*)'$url_query$k); 
    27.   $this->page = $k[2];//取得当前页的值
    28.   $url_query = ereg_replace("(^|&)page=$this->page"''$url_query);//删除查询字串中当前页的查询字段如:&page=$page或page=$page
    29.   $this->url = str_replace($parse_url['query'], $url_query$this->url);//保留其他的查询字串,
    30.   $this->page = $this->page ? $this->page : 1;//w如果查询字符串中没有当前页的值就设当前页为1
    31.   if($url_query){//如果有其他查询字符串,则以&page=$page形式添加翻页查询字串
    32.    $this->url .= '&page'
    33.   }else{//如果没有其他查询字串,则以page=$page形式添加翻页查询字串
    34.    $this->url .= 'page'
    35.   } 
    36.  }else{//如果没有查询字串,则在url后添加?page=$page形式的翻页查询字串
    37.   $this->page = 1; 
    38.   $this->url .= '?page'
    39.  } 
    40.  $this->lastpg = ceil($this->total / $this->displaypg);//计算总页数,即最后一页的页码
    41.     $this->page = min($this->lastpg, $this->page);//如果当前页大于总页数,则当前页为最后一页的页码
    42.     $this->prepg = $this->page - 1;//上一页为当前页减一www.111cn.net
    43.     $this->nextpg = $this->page + 1;//(($this->page == $this->lastpg) ? $this->lastpg : ($this->page + 1));//下一页为当前页加一,如果当前页为最后一页,则下一页为0
    44.     $this->firstcount = ($this->page - 1) * $this->displaypg;//计算当前页,记录条数开始的记录号,从0开始.
    45.  $this->startd = $this->total ? ($this->firstcount + 1) : 0;//记录开始号从1开始
    46.  $this->stopd = min($this->firstcount + $this->displaypg, $this->total);//记录结束号
    47.  //echo $this->displaypg;
    48.  //echo $this->nextpg.'+=+='.$this->lastpg;
    49. public function getpageinfo(){//取得当前页面的基本信息,如:显示第 1-10 条记录,共 23 条记录。
    50.  return '<span class="pageinfostyle">显示第<span class="numstyle">'.$this->startd.'-'.$this->stopd.'</span>条记录,共<span class="numstyle">'.$this->total.'</span>条记录。</span>'
    51. public function getcommonpagenav(){//取得通常的分页导航,如:首页 上一页 下一页 尾页
    52.  $commonnav = ''
    53.  if($this->lastpg == 1){//如果只有一页,则返回翻页导航,退出,不显示下一页,上一页等。。。
    54.   return $commonnav
    55.   break
    56.  } 
    57.  $commonnav = '<a href="'.$this->url.'=1" class="compagestyle">首页</a>';//设置首页导航,page=1
    58.  if($this->prepg){ 
    59.   $commonnav .= '<a href="'.$this->url.'='.$this->prepg.'" class="compagestyle">上一页</a>'
    60.  }else
    61.   $commonnav .= '<a class="fcompagestyle">上一页</a>'
    62.  } 
    63.  if($this->nextpg <= $this->lastpg){ 
    64.   $commonnav .= '<a href="'.$this->url.'='.$this->nextpg.'" class="compagestyle">下一页</a>'
    65.  }else
    66.   $commonnav .= '<a class="fcompagestyle">下一页</a>'
    67.  } //开源代码www.bcty365.com
    68.  $commonnav .= '<a href="'.$this->url.'='.$this->lastpg.'" class="compagestyle">尾页</a>';//显示尾页链接
    69.  return $commonnav
    70. //取得跳转分页导航,如:第n页
    71. public function getjumppagenav(){ 
    72.  //<select name='topage' size='1' onchange='window.location="/test/page.php?page="+this.value'>
    73.  $jumpnav = '<span class="pageinfostyle">到第<select name="topage" size="1" class="topage"  onchange='window.location="'.$this->url.'="+this.value'>'." "
    74.  for($i = 1; $i <= $this->lastpg; $i++){ 
    75.   if($i == $this->page){//把当前页的页码作为默认选项
    76.    $jumpnav .= '<option value="'.$i.'" selected>'.$i.'</option>'." "
    77.   }else
    78.    $jumpnav .= '<option value="'.$i.'">'.$i.'</option>'." "
    79.   } 
    80.  } 
    81.  $jumpnav .= '</select>页,共<span class="numstyle">'.$this->lastpg.'</span>页</span>'
    82.  return $jumpnav
    83. //取得所有的分页导航
    84. public function getallpagenav(){ 
    85.  $temp =  $this->getpageinfo().$this->getcommonpagenav().$this->getjumppagenav();////开源代码www.bcty365.com
    86.  return $temp
    87. //取得当前页需显示的记录,在数据库教程中的限定范围,如0-9
    88. public function getlimitstr(){ 
    89.  //echo $this->page;
    90.  //echo $this->firstcount;
    91.   
    92.  //echo $this->dispalypg;
    93.  $temp = $this->firstcount.','.$this->displaypg; 
    94.  //echo $temp;
    95.  return $temp

    使用实例,代码如下:

    1. $result=mysql_query("select * from tb_pagetest");//从数据库中查询所需显示的数据 
    2. $total=mysql_num_rows($result);//查询到的数据的总条数 
    3. $pagesize = 5;//每页显示的记录条数 
    4. $url = $_server['request_uri'];//请求的uri 
    5.   
    6. $dividepageclass = new dividepage($url$total$pagesize); //创建分页类,(类能自动初始化) 
    7. $limitstr = $dividepageclass->getlimitstr();//取得当前页要显示的记录开始序号和每页显示条数,如:0, 5(显示从0开始的5条记录) 
    8. echo $dividepageclass->getallpagenav();//显示所有分页导航条, 
    9. 如:显示第11-13条记录,共13条记录。首页 上一页 下一页 尾页 到*第 1 页,共 3 页  
    10. $sql = 'select * from tb_pagetest limit '.$limitstr;  
    11. $result=mysql_query($sql);//从数据库中取得当前页要显示的记录集,然后显示就ok 
    12. 如:  
    13. while($row=mysql_fetch_array($result))  //开源代码www.bcty365.com
    14. echo "<hr><b>".$row[title]." | ".$row[author];
    15.  
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-1545-1.html
    相关热词搜索: php分页类
    延伸阅读