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 頁碼參數
? ? ?*/
調用
//分頁 $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
總結
- 上一篇: LA 2402 (枚举) Fishnet
- 下一篇: 嵌入式实时系统程序开发一:按键程序编写