pager.php
来自「Bug tracker, and reporter.」· PHP 代码 · 共 294 行
PHP
294 行
<?phprequire_once 'Zend/View/Helper/Url.php';/*** Pager Helper*/class Zend_View_Helper_Pager{ /** * Current page * @var integer */ protected $currentPage = 1; /** * Per page * How many items to display per pages. * @var integer */ protected $perPage = 20; /** * Display links * How many page to display in the pagination links. * @var integer */ protected $pageRange = 10; /** * Total number of items * @var integer */ protected $total = 0; /** * Start page * @var integer */ protected $startPage = 1; /** * End page * @var integer */ protected $endPage = 1; /** * Pager format. */ const FORMAT_FIRST = '<a href="%s" class="pager_first">«</a>'; const FORMAT_PREVIOUS = '<a href="%s" class="pager_previous">‹</a>'; const FORMAT_PREVIOUS_RANGE = '<a href="%s" class="pager_previous_range">...</a>'; const FORMAT_NAVIGATION_LINK = '<a href="%s" class="pager_navigation_link">%d</a>'; const FORMAT_NAVIGATION_CURRENT = '<b class="pager_navigation_current">%d</b>'; const FORMAT_NEXT_RANGE = '<a href="%s" class="pager_next_range">...</a>'; const FORMAT_NEXT = '<a href="%s" class="pager_next">›</a>'; const FORMAT_LAST = '<a href="%s" class="pager_last">»</a>'; /** * Base link formats * @var array */ protected $baseFormats = array( 'first' => self::FORMAT_FIRST, 'previous' => self::FORMAT_PREVIOUS, 'previousRange' => self::FORMAT_PREVIOUS_RANGE, 'navigationLink' => self::FORMAT_NAVIGATION_LINK, 'navigationCurrent' => self::FORMAT_NAVIGATION_CURRENT, 'nextRange' => self::FORMAT_NEXT_RANGE, 'next' => self::FORMAT_NEXT, 'last' => self::FORMAT_LAST ); /** * Pager instance * @var object */ static protected $pager; /** * Pager * * @access public * * @param integer $total Total records * @param integer $perPage How many to display per page * * @return object pager */ public function pager($total = null, $perPage = 20) { if(isset(self::$pager)) return self::$pager; $page = Zend_Controller_Front::getInstance()->getRequest()->getParam('page'); if ($page > 0) { $this->currentPage = $page; } // Number of total data. $this->total = $total; // How many rows to display per page. $this->perPage = $perPage; // Number of total page $this->totalPage = ceil($this->total / $this->perPage); // If total page is smaller than called page. if ($this->totalPage < $this->currentPage) $this->currentPage = 1; // Page numnber of core pages. $corePage = ceil($this->currentPage / $this->pageRange); // Max page number. $this->endPage = ($this->pageRange * $corePage); // Start page number. $this->startPage = ($this->endPage - $this->pageRange) + 1; // Set to static instance self::$pager = $this; return $this; } /** * Display a simple pagination ex:[<< < ... 1 2 3 ... > >>] * * @param array $formats An array of links to use sprintf() * @return string A link to the desired page page of the pagination */ public function paginate(array $formats = array()) { $formats = array_merge($this->baseFormats, $formats); $this->pager($this->total, $this->perPage); $pagination = array(); $pagination[] = $this->pager()->first($formats['first']); $pagination[] = $this->pager()->previous($formats['previous']); $pagination[] = $this->pager()->previousRange($formats['previousRange']); $pagination[] = $this->pager()->navigation($formats['navigationLink'], $formats['navigationCurrent']); $pagination[] = $this->pager()->nextRange($formats['nextRange']); $pagination[] = $this->pager()->next($formats['next']); $pagination[] = $this->pager()->last($formats['last']); return join('', $pagination); } /** * Display link of the navigation links ex:[1 2 3] * * @param string $linkFormat Format string to display page number links to use sprintf() * @param string $currentFormat Format string to display current page to use sprintf() * @return string A link to the desired page page of the pagination */ public function navigation($linkFormat = self::FORMAT_NAVIGATION_LINK, $currentFormat = self::FORMAT_NAVIGATION_CURRENT) { $return = null; // Display [1,2,3__] for ($p = $this->startPage; $p <= $this->endPage; $p++) { // Unlink if the page is selected if ($p <= $this->totalPage) { if ($this->currentPage != $p) { $url = $this->url(array('page' => $p)); $return[] = sprintf($linkFormat, $url, $p); } else { $return[] = sprintf($currentFormat, $p); } } } return join('', $return); } /** * Display link of the next range ex:[...] * * @param string $format Format string of link to use sprintf() * @return string A link to the previous range of the pagination */ public function previousRange($format = self::FORMAT_PREVIOUS_RANGE) { $return = null; if ($this->pageRange < $this->currentPage) { $url = $this->url(array('page' => ($this->startPage - 1))); $return = sprintf($format, $url); } return $return; } /** * Display link of the next range ex:[...] * * @param string $format Format string of link to use sprintf() * @return string A link to the next range of the pagination */ public function nextRange($format = self::FORMAT_NEXT_RANGE) { $return = null; if ($this->endPage < $this->totalPage) { $url = $this->url(array('page' => ($this->endPage + 1))); $return = sprintf($format, $url); } return $return; } /** * Display link of the previous page ex:[<<] * * @param string $format Format string of link to use sprintf() * @return string A link to the first page of the pagination */ public function first($format = self::FORMAT_FIRST) { $return = null; if ($this->currentPage != 1) { $url = $this->url(array('page' => 1)); $return = sprintf($format, $url); } return $return; } /** * Display link of the previous page ex:[<] * * @param string $format Format string of link to use sprintf() * @return string A link to the previous page of the pagination */ public function previous($format = self::FORMAT_PREVIOUS) { $return = null; if ($this->currentPage != 1) { $url = $this->url(array('page' => ($this->currentPage - 1))); $return = sprintf($format, $url); } return $return; } /** * Display link of the next page ex:[>] * * @param string $format Format string of link to use sprintf() * @return string A link to the next page of the pagination */ public function next($format = self::FORMAT_NEXT) { $return = null; if($this->currentPage != $this->totalPage) { $url = $this->url(array('page' => ($this->currentPage + 1))); $return = sprintf($format, $url); } return $return; } /** * Display link of the last page ex:[>>] * * @param string $format Format string of link to use sprintf() * @return string A link to the last page of the pagination */ public function last($format = self::FORMAT_LAST) { $return = null; if($this->currentPage != $this->totalPage) { $url = $this->url(array('page' => $this->totalPage)); $return = sprintf($format, $url); } return $return; } /** * Generates an url given the name of a route. * * @access public * * @see Zend_View_Helper::url() * @param array $urlOptions Options passed to the assemble method of the Route object. * @param mixed $name The name of a Route to use. If null it will use the current Route * @param bool $reset Whether or not to reset the route defaults with those provided * @return string Url for the link href attribute. */ public function url(array $urlOptions = array(), $name = null, $reset = false) { $obj = new Zend_View_Helper_Url(); $url = $obj -> url($urlOptions, $name, $reset); return $url; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?