日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 分页类

發布時間:2024/1/17 php 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 分页类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<?php /**本程序文件對分頁程序進行了封裝* */class Page_Link {var $page_max = 10; //一組頁碼的最大數var $page_num = 10; //總頁數var $length = 20; //一頁的數據條數var $isNextPage = true;var $isFirstPage = false;function Calculation_Page_Num( $total ){$this->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;}// 主方法

? ? ?/*
? ? ?* $total 數據總數
? ? ?* $act_page 當前頁
? ? ?* $url 跳轉url
? ? ?* $param 頁碼參數
? ? ?*/

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;}/*** 帶上一頁<,下一頁>,省略號的分頁* @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("([?|&]$param=)", $url)) {$url = strpos($url,"?")===false?$url."?":$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;} }?>

調用

//分頁 $page=$_GET["p"]; $num=5; include "class.page.php"; $rs_total = $cls_news->get_news_count($classid);//數據總數 $total_page = ceil($rs_total / $list_news_count);//總頁數$page_cls = new Page_Link(); $page_cls->length = $num;//每頁條數 $url = "http://localhost:8001/list.php"; $page_arr = $page_cls->make_page($rs_total,$page,$url,"p");$fenye=""; foreach ($page_arr as $v){$v["url"]=$v["url"]==""?"javascript:;":$v["url"];if ($v["name"]=="Previous"){$fenye .="<a href='{$v["url"]}' class='listPrev'>{$v["name"]}</a>";}elseif($v["name"]=="Next"){$fenye .="<a href='{$v["url"]}' class='listNext'>{$v["name"]}</a>";}else{$fenye .="<a href='{$v["url"]}'>{$v["name"]}</a>";}} echo $fenye;

?

轉載于:https://www.cnblogs.com/wangxusummer/p/4149243.html

總結

以上是生活随笔為你收集整理的php 分页类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。