/* * (c) copyright Hiroya Fujii * http://blog.3ot.net/design/php/20090915185455.html * license http://opensource.org/licenses/mit-license.php MIT License * */ class Pager { // 番号リンクの数 public $linksNum = 6; // 1ページに表示するデータ数 public $parOnePage = 50; // 「いちばん最初リンク」と「いちばん最後リンク」のところの数。 public $topEnd = 2; // 「いちばん最初リンク」と「いちばん最後リンク」の区切り文字。 public $between = '...'; // prev, nextの文字列 public $prevString = 'PREV'; public $nextString = 'NEXT'; // prev, クエリ文字列 public $query = 'page'; // ulタグにつくクラス名 public $class = 'pager'; function __construct($options=false){ if(is_array($options) && count($options) > 0){ array_walk($options, array($this, 'options')); } } function doIt($total, $query=array()){ if(!$total){ return false; } $current = isset($_GET[$this->query]) ? $_GET[$this->query] : 1; $pages = ceil($total / $this->parOnePage); $prev = $current > 1 ? $current - 1 : false; $next = $current < $pages ? $current + 1 : false; $left = $current - ceil($this->linksNum/2); $right = $current + ceil($this->linksNum/2); if($left < 1){ while($right <= $this->linksNum){ $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 <= $this->topEnd; $i++){ $top[] = $i; } } $top = isset($top) ? $top : array(); if(count($top) > 0 && $top[count($top) - 1] != $temp[0] - 1){ array_push($top, $this->between); } $last = $temp[count($temp) - 1]; if($last < $pages - $this->topEnd){ array_push($temp, $this->between); } for($i = 0; $i < $this->topEnd; $i++, $pages--){ if($pages > $last){ $bottom[] = $pages; } } $bottom = isset($bottom) ? array_reverse($bottom) : array(); $temp = array_merge($top, $temp, $bottom); if($prev){ $pager[] = '