1
1

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.

ページネーションモジュール(Bootstrap対応版)

Posted at

Bootstrapで基本的な整形を済ませるのが楽だなと思うページネーションですが、その形式に吐き出してくれるPHPモジュールがなかなか見当たらなかったので作成しました。
簡易版なので、パラメータがページ部分しか対応しません。

http://hoge.com/2/
というURL形式(2の部分がページ数)で表示し、.htaccessでmod_rewriteを使いページパラメータ(ここではpage)を取得するという流れです。

定数設定

// ページネーションを展開するページのURL(スラッシュ無し)
define('SITE_URL', 'http://hoge.com');

// 1ページあたり表示件数
define('PER_PAGE', 10);
// 現在のページからの幅
define('VIEW_PAGE_MENU_WIDTH', 5);
// 「いちばん最初リンク」と「いちばん最後リンク」のところの数。
define('PAGE_TOPEND', 1);
// 「いちばん最初リンク」と「いちばん最後リンク」の区切り文字。
define('PAGE_BETWEEN', '...');
// 前ページマーク
define('PREV_MARK', '«');
// 次ページマーク
define('NEXT_MARK', '»');
// 現ページ数 GETパラメータ名
define('PAGE_QUERY', 'page');
// ULのクラス名
define('PAGER_CLASS', 'pagination');

クラスモジュール

class Pager
{

  function __construct($total)
  {
    $this->total = $total;
  }

  function getDisp($current)
  {
    $limit = PER_PAGE * $current;
    $start = (($current - 1) * PER_PAGE) + 1;
    $end = ($this->total > $limit) ? $limit : $this->total;
    return '全'.number_format($this->total).'件中 '.$start.'件から'.$end.'件表示中';
  }

  function getPagination()
  {
    if(!$this->total)
    {
      return false;
    }
    if($this->total <= PER_PAGE)
    {
      return false;
    }
    $current = isset($_GET[PAGE_QUERY]) ? $_GET[PAGE_QUERY] : 1;
    $pages = ceil($this->total / PER_PAGE);
    $prev = $current > 1 ? $current - 1 : false;
    $next = $current < $pages ? $current + 1 : false;
    $left = $current - ceil(VIEW_PAGE_MENU_WIDTH/2);
    $right = $current + ceil(VIEW_PAGE_MENU_WIDTH/2);
    if($left < 1)
    {
      while($right <= VIEW_PAGE_MENU_WIDTH)
      {
        $right++;
      }
      $left = 1;
    }
    if($right > $pages)
    {
      $left = $left - $right + $pages;
      $left = $left < 1 ? 1 : $left;
      $right = $pages;
    }
    for($i=$left; $i<=$right; $i++)
    {
      $temp[] = $i;
    }
    if($temp[0] > 1)
    {
      for($i=1; $i<$temp[0] && $i<=PAGE_TOPEND; $i++)
      {
        $top[] = $i;
      }
    }
    $top = isset($top) ? $top : array();
    if(count($top) > 0 && $top[count($top) - 1] != $temp[0] - 1)
    {
      array_push($top, PAGE_BETWEEN);
    }
    $last = $temp[count($temp) - 1];
    if($last < $pages - PAGE_TOPEND)
    {
      array_push($temp, PAGE_BETWEEN);
    }
    for($i=0; $i<PAGE_TOPEND; $i++, $pages--)
    {
      if($pages > $last)
      {
        $bottom[] = $pages;
      }
    }
    $bottom = isset($bottom) ? array_reverse($bottom) : array();
    $temp = array_merge($top, $temp, $bottom);
    if(!$prev)
    {
      $pager[] = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">'.PREV_MARK.'</span></a></li>';
    }
    else
    {
      $pager[] = '<li><a href="'.SITE_URL.'/'.$prev.'/" aria-label="Previous"><span aria-hidden="true">'.PREV_MARK.'</span></a></li>';
    }
    for($i=0; $i<count($temp); $i++)
    {
      if($temp[$i] == PAGE_BETWEEN)
      {
        $pager[] = '<li><a>'.PAGE_BETWEEN.'</a></li>';
      }
      elseif($current == $temp[$i])
      {
        $str = '<a href="'.SITE_URL.'/'.$temp[$i].'/">'.$temp[$i].'</a>';
        $pager[] = '<li class="active">'.$str.'</li>';
      }
      else
      {
        $str = '<a href="'.SITE_URL.'/'.$temp[$i].'/">'.$temp[$i].'</a>';
        $pager[] = '<li>'.$str.'</li>';
      }
    }
    if(!$next)
    {
      $pager[] = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">'.NEXT_MARK.'</span></a></li>';
    }
    else
    {
      $pager[] = '<li><a href="'.SITE_URL.'/'.$next.'/" aria-label="Previous"><span aria-hidden="true">'.NEXT_MARK.'</span></a></li>';
    }
    return '<ul class="'.PAGER_CLASS.'">'.implode($pager).'</ul>';
  }

}

使い方

$page = 現在のページ数;
$record = 全件数取得;

$pager = new Pager($record);
$pagination = $pager->getPagination();
$page_disp = $pager->getDisp($page);
<div>
  <p><?php echo $page_disp; ?></p>
  <?php echo $pagination; ?>
</div>

BootstrapのCSSとJS、及びjQueryは読み込みます。

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?