加入收藏 | 设为首页 | 会员中心 | 我要投稿 辽源站长网 (https://www.0437zz.com/)- 云专线、云连接、智能数据、边缘计算、数据安全!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

一款php分页代码

发布时间:2022-07-22 09:10:13 所属栏目:PHP教程 来源:互联网
导读:以前写过很多php 分页类但是今天这款分页程序我感觉是很好的,简洁实用,代码合理并没有多余的代码,是一款不错分页类函数哦。 class multipage { var $total; var $perpage; var $pages; var $maxpage; var $offset = 9; var $curr_page; function init($to
  以前写过很多php 分页类但是今天这款分页程序我感觉是很好的,简洁实用,代码合理并没有多余的代码,是一款不错分页类函数哦。
 
  class multipage {
   var $total;
   var $perpage;
   var $pages;
   var $maxpage;
   var $offset = 9;
   var $curr_page;
   
   function init($total, $perpage, $maxpage) { //初始化页数
    $this->total;
    $this->perpage;
    $this->maxpage;
    $this->offset = 9;
   }
   
   function getpagelist() {//获取分页列表
    $result_pages = "";
    $this->pages = ceil($this->total / $this->perpage);
     
    if ($this->pages > $this->maxpage) {
     $from = $this->curr_page - $this->offset;
     if ($from < 1) {
      $from = 1;
     }
     $to = $from + $this->maxpage - 1;
     if ($to > $this->pages) {
      $to = $this->pages;
      if (($to - $from) < $this->maxpage) {
       $from = $from - 1;
      }
     }
    } else {
     $from = 1;
     $to = $this->pages;
    }
     
    $p = 0;
    for($i = $from; $i <= $to; $i++) {
     $result_pages[$p] = $i;
     $p++;
    }
     
    return $result_pages;
   }
   
   function getfirst() { //获取第一页
    if ($this->curr_page > 1 && $this->pages > 1) {
     return 1;
    } else {
     return "";
    }
   }
   
   function getlast() { //取末页
    if ($this->pages > 1 && $this->curr_page < $this->pages) {
     return $this->pages;
    } else {
     return "";
    }
   }
   
   function getprev() {//上一页
    $prevpage = $this->curr_page - 1;
    if ($prevpage > 0) {
     return $prevpage;
    } else {
     $prevpage = "";
     return $prevpage;
    }
   }
   
   function getnext() {//下一页
    $nextpage = $this->curr_page + 1;
    if ($nextpage <= $this->pages) {
     return $nextpage;
    } else {
     $nextpage = "";
     return $nextpage;
    }
   }
   
   function gettotal() {//共多少页
    if ($this->pages > 0) {
     return $this->pages;
    } else {
     return 1;
    }
   }
   
  }
  //分页类的使用方法
  $page = new multipage();
  $page->gettotal(); //总页娄
  $page->getnext();//下一页 

(编辑:辽源站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读