3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CakePHP 2.6.10 ページング機能と検索機能

Posted at

#1.publicにcoponentsを作成

Controller
public $components = array('Paginator');
1__maxuser01_maxtestweb01__home_maxuser01_htdocs_buhinmax_app__ssh_.jpg

#2.条件設定と書き出し

Controller
// フィールド指定
$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);

その他検索条件

Controller
// 検索条件
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>
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?