PHP/Codeigniter

PHP - Paging

realizers 2021. 1. 28. 18:17
728x90
반응형

1.controllers에서 paging 라이브러리를 이용한다

● 페이징 라이브러리를 사용할 컨트롤러 생성

●$this->paging->initPaging을 사용하여 매개변수를 넘김

 

 
$this->load->library(array('Paging'));
 
$selects = array('*');
$wheres  = array();
$orders  = array('code' => 'DESC');
$limit   = array('limit' => 5, 'offset' => (5 * ($this->page - 1)));
 
$data = array();
$data['lists'] = $this->Board_model->lists($selects, $wheres, $orders, $limit);
$data['total'] = $this->Board_model->total($wheres);
 
$baseUrl  = "/admin/board/index?page=";               //현재 기본 url
$blockSize = $this->module->board_config->page_count; // 총 블록
 
$this->paging->initPaging($data['total'], $blockSize, $limit['limit'], $this->page, $baseUrl); //페이징 클래스 호출
$this->paging->getPaging();
cs

 

2.libraries에서 paging 클래스 생성

 

 
<?php
if(!defined('BASEPATH')) exit('No direct script access allowed');
Class Paging{
 
  var $total;
  var $page;
  var $limit;
  var $blockSize;
  var $baseUrl;
  var $limit_start;
  var $total_page;
  var $total_black;
  var $now_block;
  var $start_page;
  var $end_page;
  var $prev_page;
  var $next_page;
  var $pageString;
 
  function __construct(){
 
  }
 
  function initPaging($total$blockSize$limit$page$baseUrl){
 
    $this->total     = $total;     // 총 데이터 수
    $this->page      = $page;      // 현재 페이지
    $this->limit     = $limit;     // 한 페이지 출력 갯수
    $this->blockSize = $blockSize// 한 페이지 블럭 수
    $this->baseUrl   = $baseUrl;   // 기본 URL
 
    $this->total_page  = ceil($this->total / $this->limit);          // 총 페이지
    $this->total_black = ceil($this->total_page / $this->blockSize); // 총 블럭
 
    $this->now_block  = ceil($this->page / $this->blockSize);                       // 현재 페이지의 블럭
    $this->start_page = (($this->now_block * $this->blockSize) - ($this->blockSize - 1)); // 가져올 페이지의 시작번호
    $this->end_page   = ($this->now_block * $this->blockSize);                      // 가져올 마지막 페이지 번호
 
    $this->prev_page = ($this->now_block * $this->blockSize) - $this->blockSize;     // 이전 페이지
    $this->next_page = ($this->now_block * $this->blockSize) +1;                     // 다음 페이지
 
    $this->pageString = '';
  }
 
  function getPaging(){
 
    $this->pageString .= "<ul class='pagination'>";
 
      // 이전 페이지
     if($this->now_block > 1){
        $this->pageString .= "<li class='paginate_button page-item previous' id='dataTable_previous'>";
        $this->pageString .= "<a href=".$this->baseUrl.$this->prev_page." aria-controls='dataTable' class='page-link'>Previous</a>";
        $this->pageString .= "</li>";
      }
 
      // 페이지 리스트
      if($this->end_page < $this->total_page):
        $end_count = $this->end_page;
      else:
        $end_count = $this->total_page;
      endif;
 
 
      for($loop = $this->start_page; $loop <= $end_count$loop++){
         $this->pageString .= "<li class='paginate_button page-item active'>";
         $this->pageString .= "<a href=".$this->baseUrl.$loop." class='page-link'> $loop </a>";
         $this->pageString .= "</li>";
        }
 
      // 다음 페이지
      if($this->now_block < $this->total_black){
        $this->pageString .= "<li class='paginate_button page-item next' id='dataTable_next'>";
        $this->pageString .= "<a href=".$this->baseUrl.$this->next_page." aria-controls='dataTable' class='page-link'>Next</a>";
        $this->pageString .= "</li>";
      }
 
    $this->pageString .= "</ul>";
 
    return $this->pageString;
  }
}
?>
 
cs

 

3.view에서 paging 호출

1
2
3
4
5
6
7
8
<div class="row" style="width: 100%">
  <div class="col-sm-4 col-md-4"></div>
  <div class="col-sm-4 col-md-4">
    <?= $this->paging->pageString; ?>
  </div>
  <div class="col-sm-4 col-md-4"></div>
</div>
 
cs

 

3.결과

 

 

728x90
반응형