CakePHP 2.6.10 페이징 및 검색 기능

1. public에 coponents 만들기



컨트롤러
public $components = array('Paginator');



2. 조건 설정 및 내보내기



컨트롤러
// フィールド指定
$paginate['fields'][] = 'Orders.*, OrderDetails.*, Members.member_flg, Members.pref';

// Join設定
$paginate['joins'][] = array(
  'type' => 'Inner',
  'table' => 'order_detail',
  'alias' => 'OrderDetails',
  'conditions' => 'Orders.order_no = OrderDetails.order_no',
);
$paginate['joins'][] = array(
  'type' => 'Inner',
  'table' => 'Member_list',
  'alias' => 'Members',
  'conditions' => 'Members.member_id = Orders.login_id',
);

//表示数指定
$paginate['limit'] = 20;

//ソート設定
$paginate['order']['order_date'] = "desc";

//書き出し
$this->Paginator->settings = $paginate;

//存在しないページにいる場合は最初に戻す
try{
  $orders = $this->paginate('Orders', $option['conditions']);
}catch(Exception $e){
  $this->request->params['named']['page'] = 1;
  $orders = $this->paginate('Orders', $option['conditions']);
}
$this->set('Orders', $orders);

기타 검색 조건



컨트롤러
// 検索条件
if(!empty($_GET['mode']) && $_GET['mode'] == 'search'){
 // ここに条件を入れていく

  // 期間指定 //Orders.order_date を書き換え
  if(!empty($_GET['date_before']) && !empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date BETWEEN ? AND ?'] = array($_GET['date_before']." 00:00:00", $_GET['date_after']." 23:59:59");
    $getform['date_before'] = $_GET['date_before'];
    $getform['date_after'] = $_GET['date_after'];
  }elseif(!empty($_GET['date_before']) && empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date >='] = $_GET['date_before']." 00:00:00";
    $getform['date_before'] = $_GET['date_before'];
  }elseif(empty($_GET['date_before']) && !empty($_GET['date_after'])){
    $option['conditions']['Orders.order_date <='] = $_GET['date_after']." 00:00:00";
    $getform['date_after'] = $_GET['date_after'];
  }

  // フリーワード
  if(!empty($_GET['keyword'])){
    $keys = preg_split('/[\s|\x{3000}]+/u', $_GET['keyword']);
    foreach($keys as $key => $value){
      $option['conditions'][$key]['OR']['Orders.order_no like'] = "%".$value."%";
      $option['conditions'][$key]['OR']['Orders.campany_name like'] = "%".$value."%";
      $option['conditions'][$key]['OR']['Orders.order_name_sei like'] = "%".$value."%";
    }
  }
  $getform['keyword'] = $_GET['keyword'];

}

3. 양식 만들기



HTML
<!-- 検索機能 -->
<form method="get" name="search" id="search">

<div>
  <input type="hidden" name="mode" value="search">
  <h3>検索項目</h3>

  <span>登録期間:
    <input type="date" name="date_before" <?php if(!empty($getform['date_before'])){ echo 'value="'.$getform['date_before'].'"';} ?>>
     -
    <input type="date" name="date_after" <?php if(!empty($getform['date_after'])){ echo 'value="'.$getform['date_after'].'"';} ?>>
  </span>

  <span>キーワード:
    <input type="text" name="keyword" value="<?php if(!empty($getform['keyword'])){ echo $getform['keyword']; } ?>">
  </span>

  <input type="submit" value="検索">
</div>

</form>

3. 페이징 기능 만들기



HTML
<!-- ページング -->
<div id="paginate">
  <span class="prev"><?php echo $this->Paginator->prev('< 前へ', array(), null, array('class' => 'prev disabled')); ?></span>
  <span class="pageno"><?php echo $this->Paginator->numbers(array('separator' => '')); ?></span>
  <span class="next"><?php  echo $this->Paginator->next('次へ >', array(), null, array('class' => 'next disabled')); ?></span>
</div>

좋은 웹페이지 즐겨찾기