分页代码
total_rows = $data[&#39;total_rows&#39;]; $this->parameter = !empty($data[&#39;parameter&#39;]) ? $data[&#39;parameter&#39;] : &#39;&#39;; $this->list_rows = !empty($data[&#39;list_rows&#39;]) && $data[&#39;list_rows&#39;] <= 100 ? $data[&#39;list_rows&#39;] : 15; $this->total_pages = ceil($this->total_rows / $this->list_rows); $this->page_name = !empty($data[&#39;page_name&#39;]) ? $data[&#39;page_name&#39;] : &#39;p&#39;; $this->ajax_func_name = !empty($data[&#39;ajax_func_name&#39;]) ? $data[&#39;ajax_func_name&#39;] : &#39;&#39;; $this->method = !empty($data[&#39;method&#39;]) ? $data[&#39;method&#39;] : &#39;&#39;; /* 当前页面 */ if(!empty($data[&#39;now_page&#39;])) { $this->now_page = intval($data[&#39;now_page&#39;]); }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 &#39;ajax&#39;: $parameter = &#39;&#39;; if($this->parameter) { $parameter = &#39;,&#39;.$this->parameter; } return &#39;&#39; . $text . &#39;&#39; . "\n"; break; case &#39;html&#39;: $url = str_replace(&#39;?&#39;, $page,$this->parameter); return &#39;&#39; . $text . &#39;&#39; . "\n"; break; default: return &#39;&#39; . $text . &#39;&#39; . "\n"; break; } } /** * 设置当前页面链接 */ protected function _set_url() { $url = $_SERVER[&#39;REQUEST_URI&#39;].(strpos($_SERVER[&#39;REQUEST_URI&#39;],&#39;?&#39;)?&#39;&#39;:"?").$this->parameter; $parse = parse_url($url); if(isset($parse[&#39;query&#39;])) { parse_str($parse[&#39;query&#39;],$params); unset($params[$this->page_name]); $url = $parse[&#39;path&#39;].&#39;?&#39;.http_build_query($params); } if(!empty($params)) { $url .= &#39;&&#39;; } $this->url = $url; } /** * 得到$page的url * @param $page 页面 * @return string */ protected function _get_url($page) { if($this->url === NULL) { $this->_set_url(); } // $lable = strpos(&#39;&&#39;, $this->url) === FALSE ? &#39;&#39; : &#39;&&#39;; return $this->url . $this->page_name . &#39;=&#39; . $page; } /** * 得到第一页 * @return string */ public function first_page($name = &#39;第一页&#39;) { if($this->now_page > 5) { return $this->_get_link(&#39;1&#39;, $name); } return &#39;&#39;; } /** * 最后一页 * @param $name * @return string */ public function last_page($name = &#39;最后一页&#39;) { if($this->now_page <$this->total_pages - 5) { return $this->_get_link($this->total_pages, $name); } return &#39;&#39;; } /** * 上一页 * @return string */ public function up_page($name = &#39;上一页&#39;) { if($this->now_page != 1) { return $this->_get_link($this->now_page - 1, $name); } return &#39;&#39;; } /** * 下一页 * @return string */ public function down_page($name = &#39;下一页&#39;) { if($this->now_page <$this->total_pages) { return $this->_get_link($this->now_page + 1, $name); } return &#39;&#39;; } /** * 分页样式输出 * @param $param * @return string */ public function show($param = 1) { if($this->total_rows <1) { return &#39;&#39;; } $className = &#39;show_&#39; . $param; $classNames = get_class_methods($this); if(in_array($className, $classNames)) { return $this->$className(); } return &#39;&#39;; } protected function show_2() { if($this->total_pages != 1) { $return = &#39;&#39;; $return .= $this->up_page(&#39;<&#39;); for($i = 1;$i<=$this->total_pages;$i++) { if($i == $this->now_page) { $return .= "$i\n"; } else { if($this->now_page-$i>=4 && $i != 1) { $return .="...\n"; $i = $this->now_page-3; } else { if($i >= $this->now_page+5 && $i != $this->total_pages) { $return .="...\n"; $i = $this->total_pages; } $return .= $this->_get_link($i, $i) . "\n"; } } } $return .= $this->down_page(&#39;>&#39;); 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 = &#39;&#39;; $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 .= "$i\n"; } else { $return .= $this->_get_link($i, $i) . "\n"; } } $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 = &#39;总计 &#39; .$this->total_rows. &#39; 个记录分为 &#39; .$this->total_pages. &#39; 页, 当前第 &#39; . $this->now_page . &#39; 页 &#39;; $return .= &#39;,每页 &#39;; $return .= &#39; &#39;; $return .= $this->first_page()."\n"; $return .= $this->up_page()."\n"; $return .= $this->down_page()."\n"; $return .= $this->last_page()."\n"; $return .= &#39;&#39;; return $return; } }
使用方法
###处理html静态化页面分页的情况### # method 处理环境 设置为 html # parameter 为静态页面参数 xxx.com/20-0-0-0-40-?.html 注意问号 # ?问号的位置会自动替换为去向页码 # now_page 当前页面(静态页面获取不到当前页面所以只有你传入) $params = array( &#39;total_rows&#39;=>100, #(必须) &#39;method&#39; =>&#39;html&#39;, #(必须) &#39;parameter&#39; =>&#39;xxx.com/20-0-0-0-40-?.html&#39;, #(必须) &#39;now_page&#39; =>$_GET[&#39;p&#39;], #(必须) &#39;list_rows&#39; =>10, #(可选) 默认为15 ); $page = new Core_Lib_Page($params); echo $page->show(1); #2 ###处理ajax分页的情况### # method 处理环境 设置为 ajax # ajax_func_name ajax分页跳转页面的Javascript方法 # parameter ajax_func_name后面的附带参数 默认为空 # now_page 不到当前页面所以只有你传入 $params = array( &#39;total_rows&#39;=>100, &#39;method&#39; =>&#39;ajax&#39;, &#39;ajax_func_name&#39; =>&#39;goToPage&#39;, &#39;now_page&#39; =>1, #&#39;parameter&#39; =>"&#39;jiong&#39;,&#39;username&#39;", ); $page = new Core_Lib_Page($params); echo $page->show(1); #7 #添加了parameter6