Home > Article > Backend Development > A universal php paging class example code
Pagination means dividing a page into two or more pages.
There is an automatic paging mechanism that can divide the content in the mobile Web form
into a group of smaller pages for presentation, suitable for specific equipment. This mechanism also renders user interface elements that can be used to navigate to other pages.
This article mainly introduces a universal PHP paging class, which is particularly easy to use. Friends who need to use PHP paging class should not miss it. The code is as follows
init(1000, 20); * $page->setNotActiveTemplate(' {a} '); * $page->setActiveTemplate('{a}'); * echo $page->show(); * * * @author 风居住的地方 */ class Page { /** * 总条数 */ private $total; /** * 每页大小 */ private $pageSize; /** * 总页数 */ private $pageNum; /** * 当前页 */ private $page; /** * 地址 */ private $uri; /** * 分页变量 */ private $pageParam; /** * LIMIT XX,XX */ private $limit; /** * 数字分页显示 */ private $listnum = 8; /** * 分页显示模板 * 可用变量参数 * {total} 总数据条数 * {pagesize} 每页显示条数 * {start} 本页开始条数 * {end} 本页结束条数 * {pagenum} 共有多少页 * {frist} 首页 * {pre} 上一页 * {next} 下一页 * {last} 尾页 * {list} 数字分页 * {goto} 跳转按钮 */ private $template = '共有{total}条数据每页显示{pagesize}条数据,本页{start}-{end}条数据共有{pagenum}页
The second php paging class
page_num = ceil( $total / $this->length ); return $this->page_num; } function Calculation_Min_Max( $act_page = 1 ) { // 定义左右偏移量 $py_left = 0; $py_right = 0; // 定义左右边界 $bj_left = 0; $bj_right = 0; // 定义滚动区间边界 $gd_left = 0; $gd_right = 0; // 判断是否需要分组 if ( ( $this->page_num - $this->page_max ) <= 0 ) { // 不需要分组 $bj_left = 1; $bj_right = $this->page_num; } else { // 要进行分组 // 判断容量的奇偶 $tmp = $this->page_max % 2; if ( $tmp === 1 ) { // 奇数 $py_left = $py_right = ( $this->page_max - 1 ) / 2; } else { // 偶数 $py_left = $this->page_max / 2 - 1; $py_right = $this->page_max / 2; } // 计算滚动区间 $gd_left = 1 + $py_left; $gd_right = $this->page_num - $py_right; // 判断当前页是否落入了滚动区间 if ( $act_page >= $gd_left && $act_page <= $gd_right ) { // 区间内 $bj_left = $act_page - $py_left; $bj_right = $act_page + $py_right; } else { // 区间外 if ( ( $act_page - $py_left ) <= 1 ) { // 左侧固定区间 $bj_left = 1; $bj_right = $this->page_max; } else { $bj_left = $this->page_num - $this->page_max + 1; $bj_right = $this->page_num; } } } $res = array(); $res['min'] = $bj_left; $res['max'] = $bj_right; return $res; } // 主方法 function make_page( $total, $act_page, $url, $param ) { $page_num = $this->Calculation_Page_Num( $total ); $arr_min_max = $this->Calculation_Min_Max( $act_page ); if (!eregi("([?|&]$param=)", $url)) { $url = strpos($url,"?")===false?$url."?":$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("([?|&]$param=)", $url)) { $url = strpos($url,"?")===false?$url."?":$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; } ?>
The above is the detailed content of A universal php paging class example code. For more information, please follow other related articles on the PHP Chinese website!