facebook twitter hatena line google mixi email

Php/fuelphp/pagination

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

ページネーションサンプル

  • model/paginator.php
class Model_Paginator
{
   private $_contents = array();
   private $_pager;
   private $_per_page = 9; // 1ページ内のコンテンツ数
   private $_num_links = 2; // ページリンク数の半分
   private $_pagination_url;
   public function __construct($contents, $per_page = null) {
       $this->_contents = $contents;
       if ($per_page) {
           $this->_per_page = $per_page;
       }
   }
   // 日本語urlを含む場合は必ず指定
   public function setPaginationUrl($pagination_url) {
       $this->_pagination_url = $pagination_url;
   }
   private function _makePager() {
       $config = array(
           'pagination_url' => $this->_pagination_url,
           'uri_segment' => 'page',
           'per_page' => $this->_per_page,
           'num_links' => $this->_num_links,
           'total_items' => count($this->_contents),
       );
       $this->_current_page = Input::get($config['uri_segment']);
       if (!$this->_current_page) $this->_current_page = 1;
       $this->_pager = Pagination::forge('page', $config);
       // 前へaタグカスタマイズ
       $this->_pager->__set('previous-link', "\t\t<a href='{uri}' class='data-role-button'>{page}</a>\n");
       // 後へaタグカスタマイズ
       $this->_pager->__set('next-link', "\t\t<a href='{uri}' class='data-role-button'>{page}</a>\n");
       // 前へaタグ非アクティブカスタマイズ
       $this->_pager->__set('previous-inactive-link', "\t\t{page}\n");
       // 後へaタグ非アクティブカスタマイズ
       $this->_pager->__set('next-inactive-link', "\t\t{page}\n");
   }
   public function getCurrentItems() {
       return array_slice($this->_contents, $this->_per_page * ($this->_current_page - 1), $this->_per_page);
   }
   public function getPages() {
       $this->_makePager();
       return $this->_pager;
   }
}
  • controller/sample.php
$paginator = new Model_Paginator($images);
$paginator->setPaginationUrl('/word/' . urlencode($word)); // 日本語のurlを含む場合は必須。
$pager = $paginator->getPages();
$images = $paginator->getCurrentItems();
$data = array(
    'images' => $images,
    'current_page' => $pager->current_page,
    'previous_html' => $pager->previous('前'),
    'next_html' => $pager->next('後'),
);
$this->template->content = View::forge('sample/index', $data);
$this->template->content->set_safe("render_html", $pager->render());  // html自動エスケープ解除
$this->template->content->set_safe("previous_html", $pager->previous()); // html自動エスケープ解除
$this->template->content->set_safe("next_html", $pager->next()); // html自動エスケープ解除
$this->template->content->set_safe("pages_render", $pager->pages_render()); // html自動エスケープ解除
  • views/sample/index.php
<!--  « 1 2 3 4 »  -->
<?= $render_html; ?>
<!-- « -->
<?= $previous_html; ?>
<!-- » -->
<?= $next_html; ?>
<!--  1 2 3 4 -->
<?= $pages_render; ?>
<!-- 1 -->
<?= $current_page; ?>

公式paginationマニュアル

http://fuelphp.com/docs/classes/pagination.html

前と次のリンク表示を矢印から日本語へ

      // 前へaタグカスタマイズ
      $this->_pager->__set('previous-link', "\t\t<a href='{uri}' class='data-role-button'>前へ</a>\n");
      // 後へaタグカスタマイズ
      $this->_pager->__set('next-link', "\t\t<a href='{uri}' class='data-role-button'>次へ</a>\n");

bootstrap3のpaginationを使う場合

spanタグをliタグに変更

       $this->_pager->__set('regular', "<li>\n\t{link}\n</li>\n");
       $this->_pager->__set('active', "<li class='active'>\n\t{link}\n</li>\n");
       $this->_pager->__set('previous', "<li class='previous'>\n\t{link}\n</li>\n");
       $this->_pager->__set('next', "<li class='next'>\n\t{link}\n</li>\n");
       $this->_pager->__set('previous-marker', "<li class='previous'><a href='#'>前へ</a></li>\n");
       $this->_pager->__set('previous-inactive', "\n\t{link}\n\n");
       $this->_pager->__set('next-marker', "<li class='next'><a href='#'>次へ</a></li>\n");
       $this->_pager->__set('next-inactive', "\n\t{link}\n\n");