測試圖例:
效果圖:
實現代碼:
/** * * @param $page 頁碼(1至正無窮) * @param $num 數據中多上行為一頁 * @param $rows 數據的總行數 * @param $length 下標的最大長度 * @return array */ public function PageDate($page, $num , $rows , $length){ //初始化數據 $MaxPage = 0; //最大頁碼 $MinPage = 0; //最小頁碼 $ServerPage = 0; //下拉框中顯示的頁碼 $PageData = array(); //全部的頁碼集合(用戶下拉框選擇) $PageShowData = array(); //用於顯示下標的頁碼集合 $page = intval($page); if($rows == 0){ return $data = array( 'page' => $page, 'MaxPage' => $MaxPage, 'MinPage' => $MinPage, 'ServerPage' => $ServerPage, 'PageData' => $PageData, 'PageShowData' => $PageShowData ); } //得到最大分頁和最小分頁 $MaxPage = intval($rows / $num); $double = $rows % $num; if($double > 0.000000001) { $MaxPage += 1; } $MinPage = 1; $ServerPage = 1; //得到頁碼的全部項目 for($i = 1; $i <= $MaxPage; $i++ ){ $PageData[$i] = $i; } //對頁碼進行正確性改正 if($page < $MinPage){ $page = 1; } if($page > $MaxPage){ $page = 1; } /*生成顯示的頁碼標簽*/ //生成左側的代碼快 if($page == $MinPage){ $PageShowData[1] = $page; } if($page > $MinPage){ $PageShowData[1] = $MinPage; // $PageShowData[2] = $page - 1; // << $PageShowData[2] = '<<'; // << if($page -2 > $MinPage ){ $PageShowData[3] = $page - 2; $PageShowData[4] = $page - 1; }elseif($page -2 == $MinPage){ $PageShowData[3] = $page - 1; } //確定選中的頁碼代碼快 $PageShowData[count($PageShowData) + 1] = $page; } $COUNT = count($PageShowData) + 1; //生成右側的代碼塊 if($page == $MaxPage){ return $data = array( 'page' => $page, 'MaxPage' => $MaxPage, 'MinPage' => $MinPage, 'ServerPage' => $ServerPage, 'PageData' => $PageData, 'PageShowData' => $PageShowData ); } if($page < $MaxPage) { if($MaxPage - $page >= 2){ $j = 1; for($i = $COUNT; $i <= $length ; $i++){ $PageShowData[$i] = $page + $j; $j ++; if($PageShowData[$i] == $MaxPage -1){ break; } } } $COUNT = count($PageShowData) + 1; // $PageShowData[$COUNT] = $page + 1; //>> $PageShowData[$COUNT] = '>>'; $PageShowData[$COUNT + 1] = $MaxPage; //得到選中的參數 $ServerPage = $PageShowData[$COUNT-1] + 1; if($ServerPage > $MaxPage){ $ServerPage = 1; } return $data = array( 'page' => $page, 'MaxPage' => $MaxPage, 'MinPage' => $MinPage, 'ServerPage' => $ServerPage, 'PageData' => $PageData, 'PageShowData' => $PageShowData ); } }
本文出自 IT985博客