CI 묻고 답하기

제목 페이징네이션
글쓴이 승버미 작성시각 2011/11/29 14:54:45
댓글 : 6 추천 : 0 스크랩 : 0 조회수 : 20180   RSS
contoll

$this->load->helper('url');
  $this->load->library('common');
  $this->seg_exp = $this->common->segment_explode($this->uri->uri_string());
  
  if(in_array("page", $this->seg_exp)) {
   $arr_key = array_keys($this->seg_exp, "page");
   $arr_val = $arr_key[0] + 1;
   $page = $this->seg_exp[$arr_val];
   $data['page_account']=$page;
  } else {
   $page = 1;
   $data['page_account']=$page;
  }
  
  $rp = 1;
  $limit = 9;
  
  $start = (($page-1)*$rp);
  $this->load->model('table/campaign_model');
  $totalData['total'] = $this->campaign_model->find_campaign();
  $total = sizeof($totalData['total']);
  $data['search_total'] = $total;
  
  
  //검색후 페이징처리위한..
  $this->url_seg = $this->seg_exp;
  $arr_s = array_search('page', $this->url_seg);
  if($arr_s == ''){
   array_splice($this->url_seg, $arr_s, 0);
  } else {
   array_splice($this->url_seg, $arr_s, 2);
  }
  $urls = implode('/',$this->url_seg);
  
  $data['pagination_links'] = $this->common->pagination($urls."/page",$this->common->paging($page,$rp,$total,$limit),true);
  
                $this->load->view('page/default_page', $data);
 
libraries common
function segment_explode($seg) { //세크먼트 앞뒤 '/' 제거후 uri를 배열로 반환
  $len = strlen($seg);
  if(substr($seg, 0, 1) == '/') {
   $seg = substr($seg, 1, $len);
  }
  $len = strlen($seg);
  if(substr($seg, -1) == '/') {
   $seg = substr($seg, 0, $len-1);
  }
  $seg_exp = explode("/", $seg);
  return $seg_exp;
 }

 function paging($page,$rp,$total,$limit)
 {
        $limit -= 1;

        $mid = floor($limit/2);

        if ($total>$rp)
            $numpages = ceil($total/$rp);
        else
            $numpages = 1;

        if ($page>$numpages) $page = $numpages;

            $npage = $page;

        while (($npage-1)>0&&$npage>($page-$mid)&&($npage>0))
            $npage -= 1;

        $lastpage = $npage + $limit;

        if ($lastpage>$numpages)
            {
            $npage = $numpages - $limit + 1;
            if ($npage<0) $npage = 1;
            $lastpage = $npage + $limit;
            if ($lastpage>$numpages) $lastpage = $numpages;
            }

        while (($lastpage-$npage)<$limit) $npage -= 1;

        if ($npage<1) $npage = 1;

        //echo $npage; exit;

        $paging['first'] = 1;
        if ($page>1) $paging['prev'] = $page - 1; else $paging['prev'] = 1;
        $paging['start'] = $npage;
        $paging['end'] = $lastpage;
        $paging['page'] = $page;
        if (($page+1)<$numpages) $paging['next'] = $page + 1; else $paging['next'] = $numpages;
        $paging['last'] = $numpages;
        $paging['total'] = $total;
        $paging['iend'] = $page * $rp;
        $paging['istart'] = ($page * $rp) - $rp + 1;

        if (($page * $rp)>$total) $paging['iend'] = $total;

        return $paging;
 }
 
 
 function pagination($link, $paging_data) {
        $links = "";

        // The first page
        $links .= anchor($link . '/' . $paging_data['first'], 'First', array('title' => 'Go to the first page', 'class' => 'first_page'));
        $links .= "\n";
        // The previous page
        $links .= anchor($link . '/' . $paging_data['prev'], '<', array('title' => 'Go to the previous page', 'class' => 'prev_page'));
        $links .= "\n";

        // The other pages
        for ($i = $paging_data['start']; $i <= $paging_data['end']; $i++) {
            if ($i == $paging_data['page'])
                $current = '_current';
            else
                $current = "";

            $links .= anchor($link . '/' . $i, $i, array('title' => 'Go to page ' . $i, 'class' => 'page' . $current));
            $links .= "\n";
        }

        // The next page
        $links .= anchor($link . '/' . $paging_data['next'], '>', array('title' => 'Go to the next page', 'class' => 'next_page'));
        $links .= "\n";
        // The last page
        $links .= anchor($link . '/' . $paging_data['last'], 'Last', array('title' => 'Go to the last page', 'class' => 'last_page'));
        $links .= "\n";

        return $links;
    }

default_page.php

 <?=$pagination_links?>
?

페이징네이션 구현은 됫는데 여러 페이지에 페이징네이션을 넣을건데 컨트롤에 일일이 쓰자니 너무 지저분해보여서요 ㅜㅜ 간단히 불러올수 있는방법이 있을까여?
 다음글 재질문 드립니다. ci 2.1 + hmvc 5.4 (2)
 이전글 컨트롤러를에서 다른 컨트롤러 호출하는 법 있나요? (2)

댓글

변종원(웅파) / 2011/11/29 15:57:40 / 추천 0
주소처리부터 실제 페이징 만드는 함수 호출까지를 하나의 함수로 만드시면 됩니다.

그 함수 하나에 파라미터만 넘겨서 처리하게 하면 한줄로 정리하실 수 있습니다.
(어렵게 생각하실 필요가 없습니다.)
승버미 / 2011/11/29 17:51:32 / 추천 0
 
function getPaginationLinks () { 
  //주소를 불러오기위한 세팅
  $this->load->helper('url');
  $this->load->library('common');
  $this->seg_exp = $this->common->segment_explode($this->uri->uri_string());
  
  if(in_array("page", $this->seg_exp)) {
   $arr_key = array_keys($this->seg_exp, "page");
   $arr_val = $arr_key[0] + 1;
   $page = $this->seg_exp[$arr_val];
   $data['page_account']=$page;
  } else {
   $page = 1;
   $data['page_account']=$page;
  }
  
  $rp = 1; //화면에 출력할 개수
  $limit = 9; //페이지 갯수
  
  $start = (($page-1)*$rp);
  $this->load->model('table/campaign_model');
  $totalData['total'] = $this->campaign_model->find_campaign();
  $total = sizeof($totalData['total']);
  $data['search_total'] = $total;
  
  
  //검색후 페이징처리위한..
  $this->url_seg = $this->seg_exp;
  $arr_s = array_search('page', $this->url_seg);
  if($arr_s == ''){
   array_splice($this->url_seg, $arr_s, 0);
  } else {
   array_splice($this->url_seg, $arr_s, 2);
  }
  $urls = implode('/',$this->url_seg);
  
  $data['rp'] = $rp;
  $data['page'] = $this->common->pagination($urls."/page",$this->common->paging($page,$rp,$total,$limit),true);
  $data['start'] = $start;
  return $data;
 }
이런식으로 처리했습니다


$todayData['today'] = $this->campaign_model->find_campaign_page(array('cam_start_date <=' => $today , 'cam_end_date >=' => $today),'',$rp,$start);


근데 조건절로 찾아오게 되면 위에서 전체배열사이즈로 잡아서 한페이지당 1개씩출력 검색 3개가 나왔을떄  페이지수가 검색수에 맞게 안나오고 전체 페이지수로 나오네염 ㅜㅜ
페이지싸이즈 부분도 함수로 따로 만들어야하나염?

변종원(웅파) / 2011/11/29 17:57:13 / 추천 0
total은 말 그대로 전체수입니다. 검색조건이 없을때는 그 게시판의 전체게시물수,
검색조건이 있을때는 검색결과의 총수입니다.
승버미 / 2011/11/29 18:04:15 / 추천 0
 ^^ 좀더 고민해보고 해결되면 해결부분 올릴게염^^
웅파님 항상 답변 감사합니다^^
변종원(웅파) / 2011/11/29 18:12:10 / 추천 0
 쉽게 말해서 $totalData['total'] = $this->campaign_model->find_campaign();

요기에 검색조건절이 들어있는가 입니다. 파라미터 넘기는게 없어서 검색조건절이 없이 항상 전체수만 나올듯.
승버미 / 2011/11/30 11:06:59 / 추천 0
$totalData['today'] = $this->campaign_model->find_campaign(array('cam_start_date <=' => $today , 'cam_end_date >=' => $today));
  
  $data['pagenation_links'] = $this->getPaginationLinks($totalData);
function getPaginationLinks ($totalData) { 
  //주소를 불러오기위한 세팅
  if($totalData == null){
   $total = null;

  }else {
   $total = $totalData['today'];
   
  }
if($total == null) {
   $totalData['total'] = $this->campaign_model->find_campaign();   
   $total = sizeof($totalData['total']);

  }else {
   $totalData['total'] = $total;
   $total = sizeof($totalData['total']);
    
  }
요렇게 추가했음요~~

 감사욤^^