標籤:
三種不同展示方式 附上style~
1. 1
2.2
3. 3
4. 分頁類主體
<?php/** * PHP通用分頁類 * * show(2) 1 ... 62 63 64 65 66 67 68 ... 150 * 分頁樣式 * #page{font:12px/16px arial} * #page span{float:left;margin:0px 3px;} * #page a{float:left;margin:0 3px;border:1px solid #ddd;padding:3px * 7px; text-decoration:none;color:#666} * #page a.now_page,#page a:hover{color:#fff;background:#05c} */class Pager {public $first_row; //起始行數public $list_rows; //列表每頁顯示行數protected $total_pages; //總頁數protected $total_rows; //總行數protected $now_page; //當前頁數protected $method = ‘defalut‘; //處理情況 Ajax分頁 Html分頁(靜態化時) 普通get方式protected $parameter = ‘‘;protected $page_name; //分頁參數的名稱protected $ajax_func_name;public $plus = 3; //分頁位移量protected $url;/** * 建構函式 * * @param unknown_type $data */public function __construct($data = array()) {$this->total_rows = $data[‘total_rows‘];$this->parameter = !empty($data[‘parameter‘]) ? $data[‘parameter‘] : ‘‘;$this->list_rows = !empty($data[‘list_rows‘]) && $data[‘list_rows‘] <= 100 ? $data[‘list_rows‘] : 15;$this->total_pages = ceil($this->total_rows / $this->list_rows);$this->page_name = !empty($data[‘page_name‘]) ? $data[‘page_name‘] : ‘page‘;$this->ajax_func_name = !empty($data[‘ajax_func_name‘]) ? $data[‘ajax_func_name‘] : ‘‘;$this->method = !empty($data[‘method‘]) ? $data[‘method‘] : ‘‘;/* 當前頁面 */if (!empty($data[‘now_page‘])) {$this->now_page = intval($data[‘now_page‘]);} else {$this->now_page = !empty($_GET[$this->page_name]) ? intval($_GET[$this->page_name]) : 1;}$this->now_page = $this->now_page <= 0 ? 1 : $this->now_page;if (!empty($this->total_pages) && $this->now_page > $this->total_pages) {$this->now_page = $this->total_pages;}$this->first_row = $this->list_rows * ($this->now_page - 1);}/** * 得到當前串連 * * @param * $page * @param * $text * @return string */protected function _get_link($page, $text) {switch ($this->method) {case ‘ajax‘ :$parameter = ‘‘;if ($this->parameter) {$parameter = ‘,‘ . $this->parameter;}return ‘<a onclick="‘ . $this->ajax_func_name . ‘(‘‘ . $page . ‘‘‘ . $parameter . ‘)" href="javascript:void(0)">‘ . $text . ‘</a>‘ . "";break;case ‘html‘ :$url = str_replace(‘?‘, $page, $this->parameter);return ‘<a href="‘ . $url . ‘">‘ . $text . ‘</a>‘ . "";break;default :return ‘<a href="‘ . $this->_get_url($page) . ‘">‘ . $text . ‘</a>‘ . "";break;}}/** * 設定當前頁面連結 */protected function _set_url() {$url = $_SERVER[‘REQUEST_URI‘] . (strpos($_SERVER[‘REQUEST_URI‘], ‘?‘) ? ‘‘ : "?") . $this->parameter;$parse = parse_url($url);if (isset($parse[‘query‘])) {parse_str($parse[‘query‘], $params);unset($params[$this->page_name]);$url = $parse[‘path‘] . ‘?‘ . http_build_query($params);}if (!empty($params)) {$url .= ‘&‘;}$this->url = $url;}/** * 得到$page的url * * @param $page 頁面 * @return string */protected function _get_url($page) {if ($this->url === NULL) {$this->_set_url();}//$lable = strpos(‘&‘, $this->url) === FALSE ? ‘‘ : ‘&‘;return $this->url . $this->page_name . ‘=‘ . $page;}/** * 得到第一頁 * * @return string */public function first_page($name = ‘第一頁‘) {if ($this->now_page > 5) {return $this->_get_link(‘1‘, $name);}return ‘‘;}/** * 最後一頁 * * @param * $name * @return string */public function last_page($name = ‘最後一頁‘) {if ($this->now_page < $this->total_pages - 5) {return $this->_get_link($this->total_pages, $name);}return ‘‘;}/** * 上一頁 * * @return string */public function up_page($name = ‘上一頁‘) {if ($this->now_page != 1) {return $this->_get_link($this->now_page - 1, $name);}return ‘‘;}/** * 下一頁 * * @return string */public function down_page($name = ‘下一頁‘) {if ($this->now_page < $this->total_pages) {return $this->_get_link($this->now_page + 1, $name);}return ‘‘;}/** * 分頁樣式輸出 * * @param * $param * @return string */public function show($param = 1) {if ($this->total_rows < 1) {return ‘‘;}$className = ‘show_‘ . $param;$classNames = get_class_methods($this);if (in_array($className, $classNames)) {return $this->$className();}return ‘‘;}protected function show_2() {if ($this->total_pages != 1) {$return = ‘‘;$return .= $this->up_page(‘<‘);for ($i = 1; $i <= $this->total_pages; $i++) {if ($i == $this->now_page) {$return .= "<a class=‘now_page‘>$i</a>";} else {if ($this->now_page - $i >= 4 && $i != 1) {$return .= "<span class=‘pageMore‘>...</span>";$i = $this->now_page - 3;} else {if ($i >= $this->now_page + 5 && $i != $this->total_pages) {$return .= "<span>...</span>";$i = $this->total_pages;}$return .= $this->_get_link($i, $i) . "";}}}$return .= $this->down_page(‘>‘);return $return;}}protected function show_1() {$plus = $this->plus;if ($plus + $this->now_page > $this->total_pages) {$begin = $this->total_pages - $plus * 2;} else {$begin = $this->now_page - $plus;}$begin = ($begin >= 1) ? $begin : 1;$return = ‘‘;$return .= $this->first_page();$return .= $this->up_page();for ($i = $begin; $i <= $begin + $plus * 2; $i++) {if ($i > $this->total_pages) {break;}if ($i == $this->now_page) {$return .= "<a class=‘now_page‘>$i</a>";} else {$return .= $this->_get_link($i, $i) . "";}}$return .= $this->down_page();$return .= $this->last_page();return $return;}protected function show_3() {$plus = $this->plus;if ($plus + $this->now_page > $this->total_pages) {$begin = $this->total_pages - $plus * 2;} else {$begin = $this->now_page - $plus;}$begin = ($begin >= 1) ? $begin : 1;$return = ‘總計 ‘ . $this->total_rows . ‘ 個記錄分為 ‘ . $this->total_pages . ‘ 頁, 當前第 ‘ . $this->now_page . ‘ 頁 ‘;$return .= ‘,每頁 ‘;$return .= ‘<input type="text" value="‘ . $this->list_rows . ‘" id="pageSize" size="3"> ‘;$return .= $this->first_page() . "";$return .= $this->up_page() . "";$return .= $this->down_page() . "";$return .= $this->last_page() . "";$return .= ‘<select onchange="‘ . $this->ajax_func_name . ‘(this.value)" id="gotoPage">‘;for ($i = $begin; $i <= $begin + 10; $i++) {if ($i > $this->total_pages) {break;}if ($i == $this->now_page) {$return .= ‘<option selected="true" value="‘ . $i . ‘">‘ . $i . ‘</option>‘;} else {$return .= ‘<option value="‘ . $i . ‘">‘ . $i . ‘</option>‘;}}$return .= ‘</select>‘;return $return;}}?>
5.使用方法
###處理html靜態化頁面分頁的情況#### method 處理環境 設定為 html# parameter 為靜態頁面參數 xxx.com/20-0-0-0-40-?.html 注意問號# ?問號的位置會自動替換為去向頁碼# now_page 當前頁面(靜態頁面擷取不到當前頁面所以只有你傳入)$params = array(‘total_rows‘=>100, #(必須)‘method‘ =>‘html‘, #(必須)‘parameter‘ =>‘xxx.com/20-0-0-0-40-?.html‘, #(必須)‘now_page‘ =>$_GET[‘p‘], #(必須)‘list_rows‘ =>10, #(可選) 預設為15);$page = new Pager($params); echo $page->show(1); #<a href="xxx.com/20-0-0-0-40-2.html">2</a>###處理ajax分頁的情況#### method 處理環境 設定為 ajax# ajax_func_name ajax分頁跳轉頁面的javascript方法# parameter ajax_func_name後面的附帶參數 預設為空白# now_page 不到當前頁面所以只有你傳入$params = array(‘total_rows‘=>100,‘method‘ =>‘ajax‘,‘ajax_func_name‘ =>‘goToPage‘,‘now_page‘ =>1,#‘parameter‘ =>"‘jiong‘,‘username‘",);$page = new Pager($params); echo $page->show(1);#<a href="javascript:void(0)" onclick="goToPage(‘7‘)">7</a>#添加了parameter<a href="javascript:void(0)" onclick="goToPage(‘6‘,‘jiong‘,‘username‘)">6</a>
PHP通用分頁(Pager)類