CI 프레임 워 크 단순 페이지 클래스 용법 예시

본 고의 실례 는 CI 프레임 워 크 의 간단 한 페이지 분류 용법 을 서술 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.

/** 
 * 
 *                       ,     <1  >    
 * 
 */ 
class Custom_pagination 
{ 
  var $page_url = ''; //    URL 
  var $page_size = 10; //      
  var $page_num = 1;//   
  var $rows_num= '';//      
  var $links_num= 3;//          ,      1 
 
  var $anchor_class= '';//      
  var $current_class= '';//       
  var $full_tag_open= '';//       
  var $full_tag_close= '';//       
  var $info_tag_open= ''; 
  var $info_tag_close= ' '; 
  var $first_tag_open= ''; 
  var $first_tag_close= ' '; 
  var $last_tag_open= ' '; 
  var $last_tag_close= ''; 
  var $cur_tag_open= ' <strong>'; 
  var $cur_tag_close= '</strong>'; 
  var $next_tag_open= ' '; 
  var $next_tag_close= ' '; 
  var $prev_tag_open= ' '; 
  var $prev_tag_close= ''; 
  var $num_tag_open= ' '; 
  var $num_tag_close= ''; 
 
  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 ''; 
    } 
 
    $pages = intval($rows_num/$page_size); 
    if ($rows_num % $page_size) 
    { 
      //   pages+1 
      $pages++; 
    }; 
    $page_num = $this->page_num < 1 ? '1' : $this->page_num; 
 
    $anchor_class = ''; 
    if($this->anchor_class !== '') 
    { 
      $anchor_class = 'class="'.$this->anchor_class.'" '; 
    } 
 
    $current_class = ''; 
    if($this->current_class !== '') 
    { 
      $current_class = 'class="'.$this->current_class.'" '; 
    } 
    if($pages == 1) 
    { 
      return ''; 
    } 
    if($links_num < 0) 
    { 
      return '- -!links_num      0'; 
    } 
    //////////////////////////////////////////////////////// 
    //       
    //////////////////////////////////////////////////////// 
    $output = $this->full_tag_open; 
    $output .= $this->info_tag_open.' '.$rows_num.'      '.$page_num.'/'.$pages.'  '.$this->info_tag_close; 
    //   
    if($page_num > 1) 
    { 
      $output .= $this->first_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url).'" rel="external nofollow" >  </a>'.$this->first_tag_close; 
    } 
    //    
    if($page_num > 1) 
    { 
      $n = $page_num - 1; 
      $output .= $this->prev_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >   </a>'.$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.'<span '.$current_class.' >'.$i.'</span>'.$this->cur_tag_close; 
      }else if($i >= $pl && $i <= $pr) 
      { 
        $output .= $this->num_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$i).'" rel="external nofollow" >'.$i.'</a>'.$this->num_tag_close; 
      } 
    } 
    //    
    if($page_num < $pages) 
    { 
      $n = $page_num + 1; 
      $output .= $this->next_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$n).'" rel="external nofollow" rel="external nofollow" >   </a>'.$this->next_tag_close; 
    } 
    //   
    if($page_num < $pages) 
    { 
      $output .= $this->last_tag_open.'<a '.$anchor_class.' href="'.site_url($page_url.'/'.$pages).'" rel="external nofollow" >  </a>'.$this->last_tag_close; 
    } 
 
    $output.=$this->full_tag_close; 
    return $output; 
  } 
} 

컨트롤 러 호출

$config['page_url'] 
= 'about/science'; 
$config['page_size'] = $pagesize; 
$config['rows_num'] = $num_rows; 
$config['page_num'] = $page; 
$this->load->library('Custom_pagination'); 
$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['p'])&&$_GET['p']>0) ? intval($_GET['p']) : 1; 
  } 
  /** 
   *    url   GET      
   */  
  public function getUrl(){   
    $url = 'index.php?'.http_build_query($_GET); 
    $url = preg_replace('/[?,&]p=(\w)+/','',$url); 
    $url .= (strpos($url,"?") === false) ? '?' : '&'; 
    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 = ''; 
    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; 
?>

더 많은 CodeIgniter 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 이 CodeIgniter 프레임 워 크 를 바탕 으로 하 는 PHP 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기