<?php /** * 分页类 * Created by Glex * Date: 2013-02-01 * example: $pager = new Pager(100, 10); * $pager->showPager(); */ final class Pager{ private $rowSum; // 总记录数 private $row; // 每页记录数 private $pageSum; // 总页数 private $page; // 当前页 private $lang; // 语言 private $url; private $cn = array( 'records' => '条记录', 'pages' => '页', 'first' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页' ); private $en = array( 'records' => 'Records', 'pages' => 'Pages', 'first' => 'First', 'pre' => 'Pre', 'next' => 'Next', 'last' => 'Last' ); /** * 构造函数 */ function __construct($rowSum, $row = 15, $lang = 'cn', $url = '') { $this->rowSum = $rowSum; $row = ($row < 1) ? (1) : ($row); $this->row = $row; $this->pageSum = ceil($rowSum / $row); $this->page = $this->getPage(); $this->lang = $this->{$lang}; if ($url) { $this->url = $url; } else { $this->url = (isset($_SERVER['REQUEST_URI'])) ? ($_SERVER['REQUEST_URI']) : ($_SERVER['HTTP_X_REWRITE_URL']); } } /** * 分页sql */ function limit() { $start = ($this->page - 1) * $this->row; $row = $this->row; return " LIMIT $start, $row"; } /** * 获取当前页 */ private function getPage() { $page = (isset($_GET['gotopage']) && $_GET['gotopage'] > 1) ? (ceil($_GET['gotopage'])) : (1); if ($this->pageSum && $this->pageSum < $page) $page = $this->pageSum; return $page; } /** * 显示分页HTML * @param int $showtext 显示统计信息 0关闭 1开启 * @param int $shownum 显示数字索引 0关闭 1开启 * @param int $showlvtao 数字索引中间数量 最好为奇数 * @param int $showjump 显示跳转框 0关闭 1开启 * @param int $showselect 显示下拉框 0关闭 1开启 * @param int $showselects 下拉框显示数量 最好为偶数 */ function showPager($showtext = 1, $shownum = 1, $showlvtao = 7, $showjump = 1, $showselect = 0, $showselects = 10) { $rowSum = $this->rowSum; $row = $this->row; $pageSum=$this->pageSum; $page = $this->page; $page = min($pageSum, $page); $lang = $this->lang; $html = '<div id="nice-pager">'; // url $url = $this->url; $parse = parse_url($url); if (isset($parse['query'])) { parse_str($parse['query'], $params); $params['gotopage'] = ''; $url = $parse['path'] . '?' . http_build_query($params); } else{ $url.='?gotopage='; } // 统计信息 if ($showtext) { $start = ($rowSum) ? (($page - 1) * $row + 1) : (0); $end = min($page * $row, $rowSum); $html .= "<span>{$start}-{$end}/{$rowSum}{$lang['records']}</span><span>{$page}/{$pageSum}{$lang['pages']}</span>"; } if ($pageSum <= 1) return print $html . '</div>'; // 首页、上一页 $prev = $page - 1; $html .= ($prev) ? ('<a href=' . $url . '1>' . $lang['first'] . '</a>') : ('<span class="disabled">' . $lang['first'] . '</span>'); $html .= ($prev) ? ('<a href=' . $url . $prev . '>' . $lang['pre'] . '</a>') : ('<span class="disabled">' . $lang['pre'] . '</span>'); // 分页数字索引 if ($shownum) { $o = $showlvtao; $u = ceil($o / 2); $f = $page - $u; if ($f < 0) { $f = 0; } $n = $pageSum; if ($n < 1) { $n = 1; } $html .= ($page == 1) ? ('<span class="current">1</span>') : ("<a href='{$url}1'>1</a>"); // 第1位数字索引 for($i=1;$i<=$o;$i++){ if ($n <= 1) break; $c = $f + $i; if ($i == 1 && $c > 2) { $html .= '...'; } if ($c == 1) continue; if ($c == $n) break; $html .= ($c == $page) ? ('<span class="current">' . $page . '</span>') : ("<a href='{$url}{$c}'>{$c}</a>"); if ($i == $o && $c < $n - 1) { $html .= '...'; } if ($i > $n) break; } $html .= ($page == $n && $n != 1) ? ('<span class="current">' . $n . '</span>') : ("<a href='{$url}{$n}'>{$n}</a>"); // 最后1位数字索引 } // 下一页、尾页 $next = ($page == $pageSum) ? (0) : ($page + 1); $html .= ($next) ? ("<a href='{$url}{$next}'>{$lang['next']}</a>") : ("<span class='disabled'>{$lang['next']}</span>"); $html .= ($next) ? ("<a href='{$url}{$pageSum}'>{$lang['last']}</a>") : ("<span class='disabled'>{$lang['last']}</span>"); // 输入页码跳转 if ($showjump) { $html .= "<span>转到<input type='text' class='to-page' size='3' maxlength='3' title='输入页码后回车' onkeyup=\"this.value=this.value.replace(/\D/g,'')\" onafterpaste = \"this.value = this.value.replace(/\D/g,'') \" onkeydown = \"javascript:if(event.charCode==13||event.keyCode==13){if(!isNaN(this.value)){document.location.href='$url'+this.value+'';}return false;}\"/>{$lang['pages']}</span>"; } // 下拉框选页码跳转 if ($showselect) { $html .= "<span>转至<select name='topage' onchange='window.location=\"$url\"+this.value'>\n"; $lvtao = $page - $showselects / 2; $lvtaos = ($lvtao <= 0) ? (1) : ($lvtao); $lvtaoe = $page + $showselects / 2; if ($page < $showselects / 2 && $showselects <= $pageSum) { $lvtaoe = $showselects; } else if ($lvtaoe >= $pageSum) { $lvtaoe = $pageSum; } for ($i = $lvtaos; $i <= $lvtaoe; $i++) { $html .= ($i == $page) ? ("<option value='$i' selected>$i</option>\n") : ("<option value='$i'>$i</option>\n"); } $html .= "</select>{$lang['pages']}</span>"; } return print $html . '</div>'; } } ?>
<style type="text/css"> #nice-pager { padding: 10px 0; font-size: 13px; } #nice-pager span { margin: 0; padding:3px 6px 2px; } #nice-pager span.disabled { margin: 0 3px; } #nice-pager span.current,#nice-pager span.disabled { background-color: #E2E2E2; border: solid 1px #808080; border-radius: 5px; } #nice-pager a { padding: 3px 6px 2px; margin: 0 3px; background-color: #000; color:white; border: solid 1px #808080; border-radius: 5px; text-decoration: none; } #nice-pager a:hover { background-color: #E2E2E2; color:#000; } #nice-pager input.to-page { width:35px; border-radius: 6px; } </style>