>백엔드 개발 >PHP 튜토리얼 >CI 프레임워크 단순 페이징 클래스

CI 프레임워크 단순 페이징 클래스

Guanhui
Guanhui앞으로
2020-06-20 17:46:532426검색

CI 프레임워크 단순 페이징 클래스

이 문서의 예에서는 CI 프레임워크의 간단한 페이징 클래스 사용법을 설명합니다. 참조를 위해 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.


/** 
 * 
 * 关于 页码有效性的判断需要加在 控制器中判断,即当页码数<1或者>总页数 
 * 
 */ 
class Custom_pagination 
{ 
  var $page_url = &#39;&#39;; //分页目标URL 
  var $page_size = 10; //每一页行数 
  var $page_num = 1;//页码 
  var $rows_num= &#39;&#39;;//数据总行数 
  var $links_num= 3;//选中链接前后的链接数,必须大于等于1 
 
  var $anchor_class= &#39;&#39;;//链接样式类 
  var $current_class= &#39;&#39;;//当前页样式类 
  var $full_tag_open= &#39;&#39;;//分页开始标签 
  var $full_tag_close= &#39;&#39;;//分页结束标签 
  var $info_tag_open= &#39;&#39;; 
  var $info_tag_close= &#39; &#39;; 
  var $first_tag_open= &#39;&#39;; 
  var $first_tag_close= &#39; &#39;; 
  var $last_tag_open= &#39; &#39;; 
  var $last_tag_close= &#39;&#39;; 
  var $cur_tag_open= &#39; <strong>&#39;; 
  var $cur_tag_close= &#39;</strong>&#39;; 
  var $next_tag_open= &#39; &#39;; 
  var $next_tag_close= &#39; &#39;; 
  var $prev_tag_open= &#39; &#39;; 
  var $prev_tag_close= &#39;&#39;; 
  var $num_tag_open= &#39; &#39;; 
  var $num_tag_close= &#39;&#39;; 
 
  public function __construct($params = array()) 
  { 
    if (count($params) > 0) 
    { 
      $this->init($params); 
    } 
  } 
  
  function init($params = array()) //初始化数据 
  { 
    if (count($params) > 0) 
    { 
      foreach ($params as $key => $val) 
      { 
        if (isset($this->$key)) 
        { 
          $this->$key = $val; 
        } 
      } 
    } 
  } 
  
  function create_links() 
  { 
    /////////////////////////////////////////////////////// 
    //准备数据 
    /////////////////////////////////////////////////////// 
    $page_url = $this->page_url; 
    $rows_num = $this->rows_num; 
    $page_size = $this->page_size; 
    $links_num = $this->links_num; 
 
    if ($rows_num == 0 OR $page_size == 0) 
    { 
      return &#39;&#39;; 
    } 
 
    $pages = intval($rows_num/$page_size); 
    if ($rows_num % $page_size) 
    { 
      //有余数pages+1 
      $pages++; 
    }; 
    $page_num = $this->page_num < 1 ? &#39;1&#39; : $this->page_num; 
 
    $anchor_class = &#39;&#39;; 
    if($this->anchor_class !== &#39;&#39;) 
    { 
      $anchor_class = &#39;class="&#39;.$this->anchor_class.&#39;" &#39;; 
    } 
 
    $current_class = &#39;&#39;; 
    if($this->current_class !== &#39;&#39;) 
    { 
      $current_class = &#39;class="&#39;.$this->current_class.&#39;" &#39;; 
    } 
    if($pages == 1) 
    { 
      return &#39;&#39;; 
    } 
    if($links_num < 0) 
    { 
      return &#39;- -!links_num必须大于等于0&#39;; 
    } 
    //////////////////////////////////////////////////////// 
    //创建链接开始 
    //////////////////////////////////////////////////////// 
    $output = $this->full_tag_open; 
    $output .= $this->info_tag_open.&#39;共&#39;.$rows_num.&#39;条数据 第 &#39;.$page_num.&#39;/&#39;.$pages.&#39; 页&#39;.$this->info_tag_close; 
    //首页 
    if($page_num > 1) 
    { 
      $output .= $this->first_tag_open.&#39;<a &#39;.$anchor_class.&#39; href="&#39;.site_url($page_url).&#39;" rel="external nofollow" >首页</a>&#39;.$this->first_tag_close; 
    } 
    //上一页 
    if($page_num > 1) 
    { 
      $n = $page_num - 1; 
      $output .= $this->prev_tag_open.&#39;<a &#39;.$anchor_class.&#39; href="&#39;.site_url($page_url.&#39;/&#39;.$n).&#39;" rel="external nofollow" rel="external nofollow" >上一页</a>&#39;.$this->prev_tag_close; 
    } 
    //pages 
    for($i=1;$i<=$pages;$i++) 
    { 
      $pl = $page_num - $links_num < 0 ? 0 : $page_num - $links_num; 
      $pr = $page_num + $links_num > $pages ? $pages : $page_num + $links_num; 
      //判断链接个数是否太少,举例,假设links_num = 2,则链接个数不可少于 5 个,主要是 当page_num 等于 1, 2 和 n,n-1的时候 
      if($pr < 2 * $links_num + 1) 
      { 
        $pr = 2 * $links_num + 1; 
      } 
      if($pl > $pages-2 * $links_num) 
      { 
        $pl = $pages - 2 * $links_num; 
      } 
      if($i == $page_num) 
      {  //current page 
        $output .= $this->cur_tag_open.&#39;<span &#39;.$current_class.&#39; >&#39;.$i.&#39;</span>&#39;.$this->cur_tag_close; 
      }else if($i >= $pl && $i <= $pr) 
      { 
        $output .= $this->num_tag_open.&#39;<a &#39;.$anchor_class.&#39; href="&#39;.site_url($page_url.&#39;/&#39;.$i).&#39;" rel="external nofollow" >&#39;.$i.&#39;</a>&#39;.$this->num_tag_close; 
      } 
    } 
    //下一页 
    if($page_num < $pages) 
    { 
      $n = $page_num + 1; 
      $output .= $this->next_tag_open.&#39;<a &#39;.$anchor_class.&#39; href="&#39;.site_url($page_url.&#39;/&#39;.$n).&#39;" rel="external nofollow" rel="external nofollow" >下一页</a>&#39;.$this->next_tag_close; 
    } 
    //末页 
    if($page_num < $pages) 
    { 
      $output .= $this->last_tag_open.&#39;<a &#39;.$anchor_class.&#39; href="&#39;.site_url($page_url.&#39;/&#39;.$pages).&#39;" rel="external nofollow" >末页</a>&#39;.$this->last_tag_close; 
    } 
 
    $output.=$this->full_tag_close; 
    return $output; 
  } 
}

Call in the Controller


$config[&#39;page_url&#39;] 
= &#39;about/science&#39;; 
$config[&#39;page_size&#39;] = $pagesize; 
$config[&#39;rows_num&#39;] = $num_rows; 
$config[&#39;page_num&#39;] = $page; 
$this->load->library(&#39;Custom_pagination&#39;); 
$this->custom_pagination->init($config); 
echo $this->custom_pagination->create_links();


<?php 
class page{ 
   
  public $page; //当前页 
  public $pagenum; // 页数 
  public $pagesize; // 每页显示条数 
  public function __construct($count, $pagesize){ 
    $this->pagenum = ceil($count/$pagesize); 
    $this->pagesize = $pagesize; 
    $this->page =(isset($_GET[&#39;p&#39;])&&$_GET[&#39;p&#39;]>0) ? intval($_GET[&#39;p&#39;]) : 1; 
  } 
  /** 
   * 获得 url 后面GET传递的参数 
   */  
  public function getUrl(){   
    $url = &#39;index.php?&#39;.http_build_query($_GET); 
    $url = preg_replace(&#39;/[?,&]p=(\w)+/&#39;,&#39;&#39;,$url); 
    $url .= (strpos($url,"?") === false) ? &#39;?&#39; : &#39;&&#39;; 
    return $url; 
  } 
  /** 
   * 获得分页HTML 
   */ 
  public function getPage(){ 
    $url = $this->getUrl(); 
    $start = $this->page-5; 
    $start=$start>0 ? $start : 1;  
    $end  = $start+9; 
    $end = $end<$this->pagenum ? $end : $this->pagenum; 
    $pagestr = &#39;&#39;; 
    if($this->page>5){ 
      $pagestr = "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=1".">首页</a> "; 
    } 
    if($this->page!=1){ 
      $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page-1).">上一页</a>"; 
    } 
     
    for($i=$start;$i<=$end;$i++){ 
      $pagestr.= "<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$i.">".$i."</a> ";            
    } 
    if($this->page!=$this->pagenum){ 
      $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".($this->page+1).">下一页</a>"; 
       
    } 
    if($this->page+5<$this->pagenum){ 
      $pagestr.="<a href=".$url." rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" p=".$this->pagenum.">尾页</a> "; 
    } 
    return $pagestr;   
  } 
   
} 
// 测试代码 
$page = new page(100,10); 
$str=$page->getPage(); 
echo $str; 
?>

추천 튜토리얼: "PHP"


위 내용은 CI 프레임워크 단순 페이징 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 jb51.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제