DDR爱好者之家 Design By 杰米
本文为大家分享个超级好用、万能的php分页类,具体的实现代码如下
第一款php分页类
<"active"><a href="javascript:;">{text}</a></li>'; /** * 未选中的分页链接模板 */ private $notActiveTemplate = '<li><a href="{url}">{text}</a></li>'; /** * 显示文本设置 */ private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页'); /** * 初始化 * @param type $total 总条数 * @param type $pageSize 每页大小 * @param type $param url附加参数 * @param type $pageParam 分页变量 */ public function init($total, $pageSize, $param = '', $pageParam = 'page') { $this->total = intval($total); $this->pageSize = intval($pageSize); $this->pageParam = $pageParam; $this->uri = $this->geturi($param); $this->pageNum = ceil($this->total / $this->pageSize); $this->page = $this->setPage(); $this->limit = $this->setlimit(); } /** * 设置分页模板 * @param type $template 模板配置 */ public function setTemplate($template) { $this->template = $template; } /** * 设置选中分页模板 * @param type $activeTemplate 模板配置 */ public function setActiveTemplate($activeTemplate) { $this->activeTemplate = $activeTemplate; } /** * 设置未选中分页模板 * @param type $notActiveTemplate 模板配置 */ public function setNotActiveTemplate($notActiveTemplate) { $this->notActiveTemplate = $notActiveTemplate; } /** * 返回分页 * @return type */ public function show() { return str_ireplace(array( '{total}', '{pagesize}', '{start}', '{end}', '{pagenum}', '{frist}', '{pre}', '{next}', '{last}', '{list}', '{goto}', ), array( $this->total, $this->setPageSize(), $this->star(), $this->end(), $this->pageNum, $this->frist(), $this->prev(), $this->next(), $this->last(), $this->pagelist(), $this->gopage(), ), $this->template); } /** * 获取limit起始数 * @return type */ public function getOffset() { return ($this->page - 1) * $this->pageSize; } /** * 设置LIMIT * @return type */ private function setlimit() { return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}"; } /** * 获取limit * @param type $args * @return type */ public function __get($args) { if ($args == "limit") { return $this->limit; } else { return null; } } /** * 初始化当前页 * @return int */ private function setPage() { if (!empty($_GET[$this->pageParam])) { if ($_GET[$this->pageParam] > 0) { if ($_GET[$this->pageParam] > $this->pageNum) return $this->pageNum; else return $_GET[$this->pageParam]; } } return 1; } /** * 初始化url * @param type $param * @return string */ private function geturi($param) { $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "") "" : "") . $param; $parse = parse_url($url); if (isset($parse["query"])) { parse_str($parse["query"], $params); unset($params["page"]); $url = $parse["path"] . "" . http_build_query($params); return $url; } else { return $url; } } /** * 本页开始条数 * @return int */ private function star() { if ($this->total == 0) { return 0; } else { return ($this->page - 1) * $this->pageSize + 1; } } /** * 本页结束条数 * @return type */ private function end() { return min($this->page * $this->pageSize, $this->total); } /** * 设置当前页大小 * @return type */ private function setPageSize() { return $this->end() - $this->star() + 1; } /** * 首页 * @return type */ private function frist() { $html = ''; if ($this->page == 1) { $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true); } else { $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false); } return $html; } /** * 上一页 * @return type */ private function prev() { $html = ''; if ($this->page > 1) { $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false); } else { $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true); } return $html; } /** * 分页数字列表 * @return type */ private function pagelist() { $linkpage = ""; $lastlist = floor($this->listnum / 2); for ($i = $lastlist; $i >= 1; $i--) { $page = $this->page - $i; if ($page >= 1) { $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); } else { continue; } } $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true); for ($i = 1; $i <= $lastlist; $i++) { $page = $this->page + $i; if ($page <= $this->pageNum) { $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false); } else { break; } } return $linkpage; } /** * 下一页 * @return type */ private function next() { $html = ''; if ($this->page < $this->pageNum) { $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false); } else { $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true); } return $html; } /** * 最后一页 * @return type */ private function last() { $html = ''; if ($this->page == $this->pageNum) { $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true); } else { $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false); } return $html; } /** * 跳转按钮 * @return string */ private function gopage() { $html = ''; $html.=' <input type="text" value="' . $this->page . '" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')" value="GO"/>'; return $html; } /** * 模板替换 * @param type $replace 替换内容 * @param type $result 条件 * @return type */ private function replace($url, $text, $result = true) { $template = ($result "htmlcode"><"([", $url)) { $url = strpos($url,"")===false"":$url."&"; $url = $url."$param=0"; } if ( $act_page > $page_num ) { $act_page = $page_num; } // 用正则把url改成正规的 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); $res = array(); $d = 0; for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ ) { if ( $i == $act_page ) { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } $d++; } if ( $this->isNextPage ) { $res = $this->make_before_next_link( $res, $act_page, $url, $param ); } if ( $this->isFirstPage ) { $res = $this->make_first_end_link( $res, $act_page, $url, $param ); } return $res; } //// 带总页数 function make_page_with_total( $total, $act_page, $url, $param ) { $page_num = $this->Calculation_Page_Num( $total ); $arr_min_max = $this->Calculation_Min_Max( $act_page ); if (!eregi("([", $url)) { $url = strpos($url,"")===false"":$url."&"; $url = $url."$param=0"; } if ( $act_page > $page_num ) { $act_page = $page_num; } // 用正则把url改成正规的 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); $res = array(); $d = 0; for( $i = $arr_min_max['min'];$i <= $arr_min_max['max'];$i++ ) { if ( $i == $act_page ) { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } $d++; } if ( $this->isNextPage ) { $res = $this->make_before_next_link( $res, $act_page, $url, $param ); } if ( $this->isFirstPage ) { $res = $this->make_first_end_link( $res, $act_page, $url, $param ); } $total_num= ceil($total/$this->length); $result['total']=$total_num; $result['DATA']=$res; return $result; } // 附加上一页和下一页 function make_before_next_link( $arr, $act, $url, $param ) { $tmp = array(); $before = $act - 1; $next = $act + 1; if ( $before < 1 ) { $before = 1; $tmp[0]['url'] = ''; $tmp[0]['name'] = "上一页"; $tmp[0]['no'] = $before; } else { $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url ); $tmp[0]['name'] = "上一页"; $tmp[0]['no'] = $before; } $counts = sizeof( $arr ); $tmp_count = sizeof( $tmp ); for( $i = 0;$i < $counts;$i++ ) { $tmp[$tmp_count]['url'] = $arr[$i]['url']; $tmp[$tmp_count]['name'] = $arr[$i]['name']; $tmp[$tmp_count]['no'] = $arr[$i]['no']; $tmp_count++; } if ( $next > $this->page_num ) { $next = $this->page_num; $tmp[$tmp_count]['url'] = ''; $tmp[$tmp_count]['name'] = "下一页"; $tmp[$tmp_count]['no'] = $next; } else { $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url ); $tmp[$tmp_count]['name'] = "下一页"; $tmp[$tmp_count]['no'] = $next; } return $tmp; } // 附加首页和尾页 function make_first_end_link( $arr, $act, $url, $param ) { $tmp = array(); $before = 1; $next = $this->page_num; if ( $act == 1 ) { $before = 1; $tmp[0]['url'] = ''; $tmp[0]['name'] = "首页"; $tmp[0]['no'] = $before; } else { $tmp[0]['url'] = str_replace( $param . '=0', $param . '=' . $before, $url ); $tmp[0]['name'] = "首页"; $tmp[0]['no'] = $before; } $counts = sizeof( $arr ); $tmp_count = sizeof( $tmp ); for( $i = 0;$i < $counts;$i++ ) { $tmp[$tmp_count]['url'] = $arr[$i]['url']; $tmp[$tmp_count]['name'] = $arr[$i]['name']; $tmp[$tmp_count]['no'] = $arr[$i]['no']; $tmp_count++; } if ( $act == $this->page_num ) { $tmp[$tmp_count]['url'] = ''; $tmp[$tmp_count]['name'] = "尾页"; $tmp[$tmp_count]['no'] = $next; } else { $tmp[$tmp_count]['url'] = str_replace( $param . '=0', $param . '=' . $next, $url ); $tmp[$tmp_count]['name'] = "尾页"; $tmp[$tmp_count]['no'] = $next; } return $tmp; } /** * 带上一页<,下一页>,省略号的分页 * @param int $total 记录总条数 * @param int $act_page 当前页码 * @param string $url url * @param int $maxpageicon 最大显示页码数 * @param int $style 上一页,下一页显示样式 * @param string $param url参数 */ function make_page_with_points( $total,$act_page,$url,$maxpageicon,$style,$param ) { $page_num = $this->Calculation_Page_Num( $total ); //总页数 $arr_min_max = $this->Calculation_Min_Max( $act_page ); //最大页,最小页 if($total==0) { return ""; } if( $act_page > $page_num ) { $act_page = $page_num+1; $page_num = $page_num+1; } switch ($style){ case 1: $name_before = '前一页'; $name_next = '后一页'; break; case 2: $name_before = '<'; $name_next = '>'; break; case 3: $name_before = '<<'; $name_next = ''; break; default: $name_before = '上一页'; $name_next = '下一页'; } if (!eregi("([", $url)) { $url = strpos($url,"")===false"":$url."&"; $url = $url."$param=0"; } // 用正则把url改成正规的 $url = eregi_replace( $param . '=[0-9]+', $param . '=0', $url ); $res = array(); $no_before = $act_page-1; $no_next = $act_page+1; //总页数如果小于等于初始化最大呈现页数 if ($page_num<= ($maxpageicon + 1)) { //如果当前页数是首页 上一页无效 if ($act_page == 1) { $res[0]['url'] = ''; $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; } else //上一页有效 { $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; } //循环添加页码 $d = 1; for ($i = 1; $i <= $page_num; $i++) { if ($i != $act_page) { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else //当前页,页码 { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; $res[$d]['attr'] = 'current'; } $d++; } $last_d = count($res); //判断尾页 if($act_page == $page_num) //下一页无效 { $res[$last_d]['url'] = ''; $res[$last_d]['name'] = $name_next; $res[$last_d]['no'] = $no_next; } else { $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url ); $res[$last_d]['name'] = $name_next; $res[$last_d]['no'] = $no_next; } }else if ($page_num > ($maxpageicon + 1))//如果总页数满足添加省略号 { if ($act_page <= $maxpageicon) //如果当前页小于等于初始化数目 { //如果当前页数是首页 上一页无效 if ($act_page == 1) { $res[0]['url'] = ''; $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; } else //上一页有效 { $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; } //循环添加页码 $d = 1; for ($i = 1; $i <= $maxpageicon; $i++) { if ($i != $act_page) { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else //当前页,页码 { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; $res[$d]['attr'] = 'current'; } $d++; } $last_d = count($res); //添加省略号 $res[$last_d]['url'] = ''; $res[$last_d]['name'] = '...'; $res[$last_d]['no'] = ''; //总页数 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url ); $res[$last_d+1]['name'] = $page_num; $res[$last_d+1]['no'] = $page_num; //下一页 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url ); $res[$last_d+1]['name'] = $name_next; $res[$last_d+1]['no'] = $no_next; }else//如果当前页大于最大显示页面 { if ($act_page > ($page_num - $maxpageicon))//满足后几页 { //上一页 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; //第一页 $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url ); $res[1]['name'] = 1; $res[1]['no'] = 1; //省略号 $res[2]['url'] = ''; $res[2]['name'] = '...'; $res[2]['no'] = ''; $d = 3; for ($i = ($page_num - $maxpageicon + 1); $i <= $page_num; $i++) { if ($i != $act_page) { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else //当前页,页码 { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; $res[$d]['attr'] = 'current'; } $d++; } $last_d = count($res); //判断尾页 if($act_page == $page_num) //下一页无效 { $res[$last_d]['url'] = ''; $res[$last_d]['name'] = $name_next; $res[$last_d]['no'] = $no_next; } else { $res[$last_d]['url'] = str_replace( $param . '=0', $param . '=' .($act_page + 1), $url ); $res[$last_d]['name'] = $name_next; $res[$last_d]['no'] = $no_next; } }else//满足处在中间 { //上一页 $res[0]['url'] = str_replace( $param . '=0', $param . '=' .($act_page - 1), $url ); $res[0]['name'] = $name_before; $res[0]['no'] = $no_before; //第一页 $res[1]['url'] = str_replace( $param . '=0', $param . '=1', $url ); $res[1]['name'] = 1; $res[1]['no'] = 1; //省略号 $res[2]['url'] = ''; $res[2]['name'] = '...'; $res[2]['no'] = ''; for ($i = ($act_page - ($maxpageicon - 2) / 2); $i <= floor($act_page+($maxpageicon - 2) / 2); $i++) { $i = ceil($i); if ($i != $act_page) { $res[$d]['url'] = str_replace( $param . '=0', $param . '=' . $i, $url ); $res[$d]['name'] = $i; $res[$d]['no'] = $i; } else //当前页,页码 { $res[$d]['url'] = ''; $res[$d]['name'] = $i; $res[$d]['no'] = $i; $res[$d]['attr'] = 'current'; } $d++; } $last_d = count($res); //加省略号 $res[$last_d]['url'] = ''; $res[$last_d]['name'] = '...'; $res[$last_d]['no'] = ''; //当前页 $res[$last_d+1]['url'] = str_replace( $param . '=0', $param . '=' . $page_num, $url ); $res[$last_d+1]['name'] = $page_num; $res[$last_d+1]['no'] = $page_num; //下一页 $res[$last_d+2]['url'] = str_replace( $param . '=0', $param . '=' . ($act_page + 1), $url ); $res[$last_d+2]['name'] = $name_next; $res[$last_d+2]['no'] = $no_next; //exit; } } } return $res; } } ?>以上就是为大家分享的两款php分页类,希望对大家的学习有所帮助。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]