TIP게시판

제목 Customization Pagination (페이징 클래스)
글쓴이 얼짱짠군 작성시각 2012/04/02 15:43:06
댓글 : 1 추천 : 0 스크랩 : 0 조회수 : 18670   RSS
안녕하세요..
얼짱짠군입니다^^;

CI를 1년정도 사용했는데요 참편하게 코딩할수있다 생각합니다..

팁이될진모르겠지만..

게시판을 만들다가 조금 불편한부분이 있어 수정했습니다.

주소체계를 /board/test/page/10 으로 할경우

/board/test/게시물번호/page/10 과같이 중간에 게시물번호가 들어갈경우 난감합니다..

그래서 page부분을 찾아 리턴하는 부분을 조금 수정해보았습니다.

이렇게 추가할 경우 uri_segment 옵션은 필요가 없겠죠?^^;

<?
class CI_Pagination {

 # 중간코드생략;

 var $CI; //추가

 /**
  * Constructor
  *
  * @access public
  * @param array initialization parameters
  */
 public function __construct($params = array())
 {
  $this->CI =& get_instance(); //추가

  # 중간코드생략;
 }

 /**
  * Initialize Preferences
  *
  * @access public
  * @param array initialization parameters
  * @return void
  */
 function initialize($params = array())
 {
  # 중간코드생략;
 }

 // --------------------------------------------------------------------

 /**
  * uri segments the page number.
  *
  * @author csk123 (2012-04-02)
  * @access public
  * @return integer
  */
 function segments()
 {
  $segment = $this->CI->uri->segment_array();
  if($this->prefix == "P")
  {
   $matchs=0;
   foreach($segment as $keys)
   {
    if(@preg_match('/^([P]{1}|[0-9])+/', $keys))
    {
     $match=@explode("P",$keys);
     $matchs = $match[1];
    }
   }
   return $matchs?$matchs:0;
  }
  else
  {
   $keys = @array_keys($segment, $this->query_string_segment);
   if(@is_array($keys))
   {
    $match = $this->CI->uri->uri_to_assoc($keys[0]);
    return $match[$this->query_string_segment];
   }
   else
   {
    return $this->CI->uri->segment($this->uri_segment);
   }
  }
 }

 // --------------------------------------------------------------------

 /**
  * Generate the pagination links
  *
  * @access public
  * @return string
  */
 function create_links()
 {
  # 중간코드생략;

  //$CI =& get_instance(); //주석
  if ($this->CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  {
   # 중간코드생략;
  }
  else
  {
   // 기존 $this->CI->uri->segment를 새로추가한 $this->segments() 로 변경
   //if ($this->CI->uri->segment($this->uri_segment) != $base_page)
   if ($this->segments() != $base_page)
   {
    //$this->cur_page = $this->CI->uri->segment($this->uri_segment);
    $this->cur_page = $this->segments();

    // Prep the current page - no funny business!
    $this->cur_page = (int) $this->cur_page;
   }
  }
 }
}
// END Pagination Class
/* End of file Pagination.php */
/* Location: ./system/libraries/Pagination.php */

<사용방법>

1. board/test/page/10 형태
<?
class Board extends CI_Controller
{
 function Board()
 {
  parent::__construct();

  $this->load->library("pagination");
 }

 function index()
 {
  $this->lists();
 }

 function page()
 {
  $this->lists();
 }

 function lists()
 {
  // 페이지 처리
  $config["base_url"]  = '/board/test/page';
  $config["total_rows"]  = $this->board->total_rows();
  $config["num_links"]  = 10;
  $config["per_page"]  = 20;
  $config["first_link"]  = '‹ 처음';
  $config["next_link"]  = '다음 ›';
  $config["prev_link"]  = '‹ 이전';
  $config["last_link"]  = '마지막 ›';
  $config["query_string_segment"] = 'page';

  $this->pagination->initialize($config);

  if($this->pagination->segments())
  {
   $_POST["page"] = $this->pagination->segments();
  }

  $data["total_rows"] = $config["total_rows"];
  $data["pagination"] = $this->pagination->create_links();
 }
}
?>


2. board/test/P10 형태
<?
class Board extends CI_Controller
{
 function Board()
 {
  parent::__construct();

  $this->load->library("pagination");
 }

 function index()
 {
  $this->lists();
 }

 function lists()
 {
  // 페이지 처리
  $config["base_url"]  = '/board/test';
  $config["prefix"]   = 'P';
  $config["total_rows"]  = $this->board->total_rows();
  $config["num_links"]  = 10;
  $config["per_page"]  = 20;
  $config["first_link"]  = '‹ 처음';
  $config["next_link"]  = '다음 ›';
  $config["prev_link"]  = '‹ 이전';
  $config["last_link"]  = '마지막 ›';

  $this->pagination->initialize($config);

  if($this->pagination->segments())
  {
   $_POST["page"] = $this->pagination->segments();
  }

  $data["total_rows"] = $config["total_rows"];
  $data["pagination"] = $this->pagination->create_links();
 }
}
?>

* 소스코드는 압축해서 첨부했습니다.

허접하지만 간단한 팁이였어요;;

이상입니다.

첨부파일 system.zip (3.0 KB)
 다음글 CLI 모드로 실행할 때 로그파일 관련문제 (1)
 이전글 db library 에서 save_queries 옵션에... (3)

댓글

한대승(불의회상) / 2012/04/03 17:07:45 / 추천 0
수고 하셨습니다.

페이징이 국내 사용방법과 달라 조금 혼동 되는 부분이 있었는데 도움이 많이 될것 같습니다.