忍者ブログ
自分なりの目線で情報を発信します。
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

Zend_Paginatorのサンプル続編です。
前回は、単純なページングのサンプルのみでしたが、
今回は検索条件(検索文字)付きのページングサンプルを紹介します。
検索機能付きの一覧画面が必要になることは日常茶飯事ですよねw
 
 
以下のテーブルからデータを取得して一覧を表示します。
 
■テーブル名
user
■カラム定義
id INT
name VARCHAR(16)
email VARCHAR(320)
 
※カラムの名前のつけ方が良くないのはサンプルなのでご容赦を。
 id、nameなんて名前は、個人的には実際には使わない方がいいと思います。
  (例えば、HTMLのinputタグ等の属性と同じ名前だし・・・orz)
 
検索に使えるのは、以下4項目です。
  • ID (user.idに対応)
  • 名前 (user.nameに対応)
  • メールアドレス (user.emailに対応)
  • 表示件数 (対応カラム無し、1ページあたりの表示件数を指定)
 
■UserController.php
  1. <?php
  2.  
  3. class UserController extends Zend_Controller_Action
  4. {
  5.     // ユーザー一覧表示
  6.     public function userListAction()
  7.     {
  8.         // ページ番号のリクエストがあれば取得(無ければ1ページ目とする)
  9.         $pagenum   = $this->getRequest()->getParam('page', 1);
  10.  
  11.         // DBアダプタ生成
  12.         $dbAdapter = new Zend_Db_Adapter_Pdo_Mysql(array(
  13.                              'host'     => 'localhost',
  14.                              'username' => 'xxxx',
  15.                              'password' => 'xxxx',
  16.                              'dbname'   => 'xxxx'
  17.                           ));
  18.  
  19.         // 検索条件リクエスト値を取得
  20.         $searchCond['id']         = $this->getRequest()->getParam('id', null);
  21.         $searchCond['name']       = $this->getRequest()->getParam('name', null);
  22.         $searchCond['email']      = $this->getRequest()->getParam('email', null);
  23.         $searchCond['show_count'] = $this->getRequest()->getParam('show_count', 10);
  24.  
  25.         // SQL作成
  26.         $select = $dbAdapter->select()
  27.                             ->from(array('usr' => 'user'),
  28.                                    array('id', 'name', 'email', 'regdate'))
  29.                             ->order(array('id'));
  30.         // SQLに検索条件を追加
  31.         foreach ($this->searchCond as $colname => $colval) {
  32.             if (is_null($val) || $val === '') {
  33.                 continue;
  34.             }
  35.             switch ($key) {
  36.                 case 'id':
  37.                     $select->where("{$colname} = ?", $colval);
  38.                     break;
  39.                 case 'name':
  40.                 case 'email':
  41.                     $select->where("{$colname} LIKE ?", "%{$colval}%");
  42.                     break;
  43.                 default:
  44.                     break;
  45.             }
  46.         }
  47.  
  48.         // ページネーターを取得
  49.         $adapter   = new Zend_Paginator_Adapter_DbSelect($select);
  50.         $paginator = new Zend_Paginator($adapter);
  51.         // 現在ページを設定
  52.         $paginator->setCurrentPageNumber($pagenum);
  53.         // 1ページあたりの表示件数を設定(リクエストされた値を渡す)
  54.         $paginator->setItemCountPerPage($searchCond['show_count']);
  55.         // ビューにページネーターを渡す
  56.         $this->view->assign('userPaginator', $paginator);
  57.         // ビューを表示
  58.         $this->renderScript('admin/user_list.php');
  59.     }
  60. }
  61.  
  62. // end of file
リクエストを受け付けるアクションです。
テーブルからデータを取得するZend_Paginatorを、 ビューに渡しています。
検索条件として入力された検索文字のリクエスト値を取得し、
値が入力された場合のみwhere条件に追加しています。

Zend_Db_Selectオブジェクトを使っているため、
whereメソッドを呼び出すだけで条件に追加できます。
whereメソッドを複数回呼び出すと、それぞれの条件はANDで繋がります。
OR条件で繋げたい場合は、orWhereメソッドを使います。
詳しくはこちらを参照してください。
 
 
 
■user_list.php
  1. <html>
  2. <head>
  3.   <title>ユーザー一覧</title>
  4.   <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  5. </head>
  6. <body>
  7.   <h1>ユーザー一覧</h1>
  8.   <form action="<?php
  9.                   echo $this->url(
  10.                          array('controller'=>'user', 'action'=>'user-list'),
  11.                                '',
  12.                                TRUE);
  13.                 ?>" method="get">
  14.     <table>
  15.       <thead></thead><tfoot></tfoot>
  16.       <tbody>
  17.         <tr>
  18.           <th>ID</th>
  19.           <td>
  20.             <input type="text" name="id" id="id"
  21.                value="<?php echo htmlspecialchars($this->searchCond['id'], ENT_QUOTES); ?>">
  22.           </td>
  23.         </tr>
  24.         <tr>
  25.           <th>名前</th>
  26.           <td>
  27.             <input type="text" name="name" id="name"
  28.                value="<?php echo htmlspecialchars($this->searchCond['name'], ENT_QUOTES); ?>">
  29.           </td>
  30.         </tr>
  31.         <tr>
  32.           <th>メールアドレス</th>
  33.           <td>
  34.             <input type="text" name="email" id="email"
  35.                value="<?php echo htmlspecialchars($this->searchCond['email'], ENT_QUOTES); ?>">
  36.           </td>
  37.         </tr>
  38.         <tr>
  39.           <th>表示件数</th>
  40.           <td>
  41.           <select name="show_count">
  42.             <?php $options = array('10', '20', '50', '100'); ?>
  43.             <?php foreach ($options as $optkey => $optval): ?>
  44.               <?php if ($optval === $this->searchCond['show_count']): ?>
  45.                 <option value="<?php echo $optval; ?>" selected="selected">
  46.                   <?php echo $optval; ?>
  47.                 </option>
  48.               <?php else: ?>
  49.                 <option value="<?php echo $optval; ?>">
  50.                   <?php echo $optval; ?>
  51.                 </option>
  52.               <?php endif; ?>
  53.             <?php endforeach; ?>
  54.           </select>
  55.           </td>
  56.         </tr>
  57.       </tbody>
  58.     </table>
  59.     <input type="button" id="btn_search" value="検索">
  60.   </form>
  61.   <br>
  62.   <table summary="ユーザー一覧">
  63.     <thead>
  64.       <tr>
  65.         <th>ID</th>
  66.         <th>名前</th>
  67.         <th>メールアドレス</th>
  68.       </tr>
  69.     </thead>
  70.     <tfoot></tfoot>
  71.     <tbody>
  72.     <?php foreach($this->userPaginator as $key => $user): ?>
  73.       <tr>
  74.         <td>
  75.           <?php echo $user['id']; ?>
  76.         </td>
  77.         <td>
  78.           <?php echo $user['name']; ?>
  79.         </td>
  80.         <td>
  81.           <?php echo $user['email']; ?>
  82.         </td>
  83.       </tr>
  84.     <?php endforeach; ?>
  85.     <?php if (count($this->userPaginator) === 0): ?>
  86.       <tr>
  87.         <td colspan="3">
  88.           データがありません
  89.         </td>
  90.       </tr>
  91.     <?php endif; ?>
  92.     </tbody>
  93.   </table>
  94.   <!-- ページネーションリンクを表示 -->
  95.   <?php echo $this->paginationControl(
  96.                 $this->inquiryPaginator,
  97.                 'Sliding',
  98.                 'pagination/userlist_pagination.phtml',
  99.                 array(
  100.                     'controller' => 'user',
  101.                     'action'     => 'user-list',
  102.                     'id'         => $this->searchCond['id'],
  103.                     'name'       => $this->searchCond['name'],
  104.                     'email'      => $this->searchCond['email'],
  105.                     'show_count' => $this->searchCond['show_count']
  106.                     )
  107.                 );
  108.   ?>
  109. </body>
  110. </html>
一覧表示用のビュースクリプトです。
コントローラーから渡されたページネーターを使って、一覧を表示しています。
 
83~96行目に、ページネーションリンクを表示するための記述をしています。
引数は順番に、
 Zend_Paginatorのインスタンス
 スクロール形式
 ビュースクリプト
  オプション
です。
検索文字はすべて第4引数のオプションとして渡してしまえばいいです。
 
 
■userlist_pagination.phtml
  1. <div class="paginationControl">
  2. <?php
  3.     $urlparams = array(
  4.                      'controller' => $this->controller,
  5.                      'action'     => $this->action,
  6.                      'page'       => '',
  7.                      'id'         => $this->id,
  8.                      'name'       => $this->name,
  9.                      'email'      => $this->email,
  10.                      'show_count' => $this->show_count
  11.                  );
  12. ?>
  13. <?php echo '全'.$this->totalItemCount.'件'; ?>&nbsp;
  14. <?php if ($this->pageCount > 1): ?>
  15.   <!-- 前のページへのリンク -->
  16.   <?php if (isset($this->previous)): ?>
  17.     <?php $urlparams['page'] = $this->previous; ?>
  18.     <a href="<?php echo $this->url($urlparams); ?>">
  19.       &lt; 前へ
  20.     </a> |
  21.   <?php else: ?>
  22.     <span class="disabled">&lt; 前へ</span> |
  23.   <?php endif; ?>
  24.  
  25.   <!-- ページ番号へのリンク -->
  26.   <?php foreach ($this->pagesInRange as $page): ?>
  27.     <?php if ($page != $this->current): ?>
  28.       <?php $urlparams['page'] = $page; ?>
  29.       <a href="<?php echo $this->url($urlparams); ?>">
  30.           <?php echo $page; ?>
  31.       </a> |
  32.     <?php else: ?>
  33.       <?php echo $page; ?> |
  34.     <?php endif; ?>
  35.   <?php endforeach; ?>
  36.  
  37.   <!-- 次のページへのリンク -->
  38.   <?php if (isset($this->next)): ?>
  39.     <?php $urlparams['page'] = $this->next; ?>
  40.     <a href="<?php echo $this->url($urlparams); ?>">
  41.       次へ &gt;
  42.     </a>
  43.   <?php else: ?>
  44.     <span class="disabled">次へ &gt;</span>
  45.   <?php endif; ?>
  46. <?php endif; ?>
  47. </div>
ページネーションリンク用のビュースクリプトです。
以前紹介したサンプルとは違い、user_list.phpから受け取った検索文字を
各ページネーションリンクに渡しています。
$this->url()メソッドに配列形式で検索文字を渡すことで、
[配列のキー]=[配列の値]
形式のURLパラメータを付加したURLを返してくれます。
 
 
検索文字付きのページネーションも、
ビューヘルパーをいくつか組み合わせるだけで簡単にできますね。
 
 
PR
この記事にコメントする
お名前
タイトル
文字色
メールアドレス
URL
コメント
パスワード   Vodafone絵文字 i-mode絵文字 Ezweb絵文字
お知らせ
プロフィール
HN:
shusatoo
性別:
男性
自己紹介:
基本PHP。JavaScriptちょっとだけ。Javaをほんの少し。
おすすめ本
最新コメント
[10/09 shusatoo]
[10/09 misney]
最新トラックバック
カレンダー
03 2024/04 05
S M T W T F S
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
ブログ内検索
カウンター
アク解アナライズ
バーコード
ビジター

Copyright © [ 開発メモるアル ] All rights reserved.
Special Template : 忍者ブログ de テンプレート
Special Thanks : 忍者ブログ
Commercial message : [PR]