メソッドチェーンを使った自作ページャークラス

特徴

  • 「前へ」「次へ」「最初へ」「最後へ」の相対表現のリンク、全ページ番号への直接リンク、全ページ数、現在表示中の項目の番号に対応。
  • 絞り込み検索等でもページング出来るようにGETのクエリパラメータを保持出来る。
  • メソッドチェーンで直感的(?)にページ情報を取り出せる。

使い方

下のようにPagerクラスのインスタンス$pagerを作り、

 <?php
 $pager = new Pager(array(
   'cur_no'       => 2,                                // (1)
   'total'        => 45,                               // (2)
   'num_per_page' => 10,                               // (3) 
   'path'         => 'http://abc.com/index.php?flg=1', // (4)
   'query' => array('year' => 2010, 'month' => 9)      // (5)
 ));

コンストラクタの引数

  • (1) 現在のページ番号
  • (2) 全項目数
  • (3) 1ページに表示する項目数
  • (4) ページのURL(http(s)から始まるURLでも、/から始まるURLでも、クエリパラメータを付けてもOK)
  • (5) クエリーパラメータに加えたいハッシュ情報(絞り込み検索結果をページングする場合を想定)

↓のようにHTML内で$pagerからページ情報を取り出す。例えば「次のページ」のURLは$pager->next()->url()で、ページ番号は$pager->next()->no()のようにして。

 <ul>
   <li><a href="<?= $pager->first()->url() ?>">最初へ</a></li>
   <li><a href="<?= $pager->prev()->url() ?>">前へ</a></li>
   <li>
     <ul>
 <?php foreach ($pager->all() as $p) { ?>
 <?php if ($pager->no() == $p->no()) { ?>
         <li><?= $p ?>ページ</li>
 <?php } else { ?>
         <li><a href="<?= $p->url() ?>"><?= $p ?>ページへ</a></li>
 <?php } ?>
 <?php } ?>
     </ul>
   </li>
   <li><a href="<?= $pager->next()->url() ?>">次へ</a></li>
   <li><a href="<?= $pager->last()->url() ?>">最後へ</a></li>
   <li>全<?= $pager->last() ?>ページ中 <?= $pager->cur() ?>ページ 表示</li>
   <li>全<?= $pager->total() ?>項目中 <?= $pager->cur()->firstIndex() ?>から<?= $pager->cur()->lastIndex() ?>番目の項目 表示</li>
 </ul>

これで下のようなHTMLになる。

 <ul>
   <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=1">最初へ</a></li>
   <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=1">前へ</a></li>
   <li>
     <ul>
         <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=1">1ページへ</a></li>
         <li>2ページ</li>
         <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=3">3ページへ</a></li>
         <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=4">4ページへ</a></li>
         <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=5">5ページへ</a></li>
     </ul>
   </li>
   <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=3">次へ</a></li>
   <li><a href="http://abc.com/index.php?flg=1&year=2010&month=9&no=5">最後へ</a></li>
   <li>全5ページ中 2ページ 表示</li>
   <li>全45項目中 11から20番目の項目 表示</li>
 </ul>

ページャークラスのソースコード

 class Pager {
     private $total;
     private $num_per_page;
     private $cur_no;
     private $first_no;
     private $last_no;
     private $next_no;
     private $prev_no;
     private $query = array();
     private $no;
  
     function Pager($params) {
         $total        = $params['total'];
         $num_per_page = $params['num_per_page'];
         $cur_no       = $params['cur_no'];
         $path         = isset($params['path']) ? $params['path'] : '';
         $query        = isset($params['query']) ? $params['query'] : array();
  
         $first_no = 1;
         $last_no  = ceil($total / $num_per_page);
         $next_no  = $cur_no < $last_no  ? $cur_no + 1 : null;
         $prev_no  = $cur_no > $first_no ? $cur_no - 1 : null;
         $all_nos  = array();
         for ($i = $first_no; $i <= $last_no; $i++) {
             $all_nos[] = $i;
         }
         $this->total        = $total;
         $this->num_per_page = $num_per_page;
         $this->cur_no       = $cur_no;
         $this->first_no     = $first_no;
         $this->last_no      = $last_no;
         $this->next_no      = $next_no;
         $this->prev_no      = $prev_no;
         $this->all_nos      = $all_nos;
         $this->path         = $path;
         $this->query        = $query;
         $this->no           = $cur_no;
     }
  
     function all() {
         $buf = array();
         $all_nos = $this->all_nos;
         foreach ($all_nos as $no) {
             $buf[] = new Pager(array(
                 'cur_no'       => $no,
                 'total'        => $this->total,
                 'num_per_page' => $this->num_per_page,
                 'path'         => $this->path,
                 'query'        => $this->query
             ));
         }
         return $buf;
     }
  
     function cur() { 
         $this->no = $this->cur_no;
         return $this;
     }
  
     function total() { return $this->total; }
  
     function first() { 
         $this->no = $this->first_no;
         return $this;
     }
  
     function last() { 
         $this->no = $this->last_no;
         return $this;
     }
  
     function next() {
         $this->no = $this->next_no;
         return $this;
     }
  
     function prev() {
         $this->no = $this->prev_no;
         return $this;
     }
  
     function no() {
         return $this->cur_no;
     }
  
     function firstIndex() {
         $idx = $this->num_per_page * ($this->no - 1) + 1;
         if ($idx > $this->total) $idx = $this->total;
         return $idx;
     }
  
     function lastIndex() {
         $idx = $this->num_per_page * $this->no;
         if ($idx > $this->total) $idx = $this->total;
         return $idx;
     }
  
     function url($ssl = false) {
        if (!$this->no) return '';
         $url_info = parse_url($this->path);
         if ($ssl) $url_info['scheme'] = 'https';
         $query = array();
         if (isset($url_info['query']) && $url_info['query'] != '') {
             parse_str($url_info['query'], $query);
         }
         $query = array_merge($query, $this->query);
         $query['no'] = $this->no;
         $buf = array();
         foreach ($query as $k => $v) {
             $buf[] = sprintf('%s=%s', urlencode($k), urlencode($v));
         }
         $url_info['query'] = join('&', $buf);
         if (isset($url_info['scheme']) &&isset($url_info['host']) ) {
             $url = sprintf('%s://%s%s%s%s',
                         $url_info['scheme'],
                         $url_info['host'],
                         $url_info['path'],
                         $url_info['query'] ? '?' . $url_info['query'] : '',
                         isset($url_info['fragment']) && $url_info['fragment'] != '' ? '#' . $url_info['fragment'] : ''
                     );
         } else {
             $url = sprintf('%s%s%s',
                         $url_info['path'],
                         $url_info['query'] ? '?' . $url_info['query'] : '',
                         isset($url_info['fragment']) && $url_info['fragment'] != '' ? '#' . $url_info['fragment'] : ''
                     );
         }
         return $url;
     }
  
     function __toString() {
         return (string) $this->no;
     }
 }

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2010-09-17 (金) 20:20:20