FuelPHPのページネーション

作成日:2018-04-02, 更新日:2018-08-17

基本

1.「core/config/pagination.php」を「app/config」内に複製
2.使いたいデザインを設定

使いたいデザインを設定

▼「app/config/pagination.php」の下記を修正

'active' => '〇〇〇〇',

「'active'」の項目の下に各デザインがあるのでそれを使う。「default、bootstrap3、bootstrap」など。

もし、既存分が何かしらの理由で使えない場合はそれぞれを参考に自作する。

bootstrapの4を使うときのひな型

'active'                     => 'bootstrap4',

'bootstrap4'                  => array(
  'wrapper'                => "<ul class=\"pagination\">\n\t{pagination}\n\t</ul>\n",

  'first'                  => "\n\t\t<li class=\"page-item\">{link}</li>",
  'first-marker'           => "««",
  'first-link'             => "<a href=\"{uri}\" class=\"page-link\">{page}</a>",

  'first-inactive'         => "",
  'first-inactive-link'    => "",

  'previous'               => "\n\t\t<li class=\"page-item\">{link}</li>",
  'previous-marker'        => "«",
  'previous-link'          => "<a class=\"page-link\" href=\"{uri}\" rel=\"prev\">{page}</a>",

  'previous-inactive'      => "\n\t\t<li class=\"page-item disabled\">{link}</li>",
  'previous-inactive-link' => "<a class=\"page-link\" href=\"#\" rel=\"prev\">{page}</a>",

  'regular'                => "\n\t\t<li class=\"page-item\">{link}</li>",
  'regular-link'           => "<a class=\"page-link\" href=\"{uri}\">{page}</a>",

  'active'                 => "\n\t\t<li class=\"page-item active\">{link}</li>",
  'active-link'            => "<a class=\"page-link\" href=\"#\">{page} <span class=\"sr-only\"></span></a>",

  'next'                   => "\n\t\t<li class=\"page-item\">{link}</li>",
  'next-marker'            => "»",
  'next-link'              => "<a class=\"page-link\" href=\"{uri}\" rel=\"next\">{page}</a>",

  'next-inactive'          => "\n\t\t<li class=\"page-item disabled\">{link}</li>",
  'next-inactive-link'     => "<a class=\"page-link\" href=\"#\" rel=\"next\">{page}</a>",

  'last'                   => "\n\t\t<li class=\"page-item\">{link}</li>",
  'last-marker'            => "»»",
  'last-link'              => "<a class=\"page-link\" href=\"{uri}\">{page}</a>",

  'last-inactive'          => "",
  'last-inactive-link'     => "",
),

ページネーションの出力方法

随分前にベースを作ったときに色々とメモってたけど・・・そのメモが壊れてしまって・・・詳細不明。調べればわかるんだけど、面倒なので調べない。

出力データの作成:オレオレライブラリ

▼ひとまず現在追加回しているファイル:app/classes/util/search.php

<?php
class Util_Search {
  /** ページネーションの作成 **/
  public static function getPagination($uri, $total, $getPrm="", $prms=array()) {
    $result = array(
       "htmlPge" => "",     // ページネーションのHTML
       "nums"    => "",     // 表示件数
       "viewFst" => "",     // 表示している最初の件数
       "viewEnd" => "",     // 表示している最後の件数
       "total"   => $total, // 合計
       "fstIdx"  => 0,      // limit句:offset
       "endNum"  => 0,      // limit句:per_page
    );
    
    $instance = 'navWrap';
    
    $tmpPagination = self::setPagination($uri, $total, $getPrm, $instance, $prms);
    $pagination = $tmpPagination->instance($instance);
    
    $nowPage = (int) $pagination->calculated_page;
    $perPage = (int) $pagination->per_page;
    
    $result["htmlPge"] = $pagination->render(false);
    
    $result["nums"] = $perPage;
    
    $result["viewFst"] = ( $nowPage - 1 )  * $perPage + 1;
    
    $tmpViewEnd = $nowPage * $perPage;
    if ( $tmpViewEnd < $total ) {
      $result["viewEnd"] = $tmpViewEnd;
    }
    else {
      $result["viewEnd"] = $total;
    }
    
    $result["fstIdx"] = $pagination->offset;
    $result["endNum"] = $pagination->per_page;
    
    return $result;
  }
  
  /** ページネーションの初期設定 **/
  public static function setPagination($uri, $count=0, $getPrm="", $instance='myPN', $prms=array()) {
    // 表示件数
    if ( isset($prms['nums']) ) {
      $nums = (int) $prms['nums'];
    }
    else {
      $nums = \Input::get("v", 20);
    }
    
    if ( $getPrm != "" ) {
      $getPrm .= '&v=' . $nums;
    }
    else {
      $getPrm = 'v=' . $nums;
    }
    
    $config = self::getStructurePagination($uri, $nums, $count, $getPrm);
    
    return \Pagination::forge($instance, $config);
  }
  
  /** ページネーション **/
  public static function getStructurePagination($uri, $nums=10, $count=0, $getPrm="") {
    
    if ( $getPrm != "" ) {
      $tmpUri = \Uri::create($uri) . '?' . $getPrm;
    }
    else {
      $tmpUri = \Uri::create($uri);
    }
    
    return array(
       'pagination_url' => $tmpUri, // 
       'uri_segment'    => "p",     // GETパラメータ
       'num_links'      => 5,       // 表示するリンクの総数(奇数)
       'per_page'       => $nums,   // 1 ページに表示するレコード数
       'total_items'    => $count,  // アイテムの総数
       'show_first'     => true,    // 「最初のページヘ」リンクの有無
       'show_last'      => true,    // 「最後のページヘ」リンクの有無
       
       "first-marker"    => '&laquo;',
       "previous-marker" => '&lsaquo;',
       "next-marker"     => '&rsaquo;',
       "last-marker"     => '&raquo;',
    );
  }
}

出力データの呼び出し

▼コントローラーか、適当なトコで呼び出し。

$url     = URL
$uriPrms = URLにつくGETパラメータ
$total   = 総数
$opt     = array(
  'nums' => \Input::get('v', 10), // 表示件数のデフォ値
);

$pagiNation = \Util_Search::getPagination($path, $arySearch['total'], $prms['uriPrms'], $opt);

// var_dump($pagiNation); // 下記のような感じで返ってくるので、「htmlPge」をビュー側で出力する。
// Array(
//   htmlPge => ページネーションのHTML
//   nums    => 3
//   viewFst => 1
//   viewEnd => 3
//   total   => 8
//   fstIdx  => 0
//   endNum  => 3
// )