這篇文章主要介紹了PHP實現仿Google分頁效果的分頁函數,實例分析了php實現分頁的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了PHP實現仿Google分頁效果的分頁函數。分享給大家供大家參考。具體如下:
- /**
- * 分頁函數
- * @param int $total 總頁數
- * @param int $pagesize 每頁幾條
- * @param string $pkey url中頁面變量名稱
- * @param string $url 基礎url,其中的{page}會被替換為頁碼
- * 依賴函數 request_uri httpInt
- */
- function page($total,$pagesize=10,$pkey='p',$url=null){
- $a_num=10;
- $first=' 首頁 ';
- $last=' 尾頁 ';
- $pre=' 上頁 ';
- $next=' 下頁 ';
- $a_num=$a_num%2==0?$a_num+1:$a_num;
- if(!$url){
- $url=preg_replace("/&?{$pkey}=[^&]{0,}/", '',request_uri());
- $url=strpos($url, '?')===FALSE?$url.'?'.$pkey.'={page}':$url.'&'.$pkey.'={page}';
- }
- $pages=ceil($total/$pagesize);
- $curpage=httpInt($pkey,false,1);
- $curpage=$curpage>$pages||$curpage<=0?1:$curpage;//當前頁超范圍置為1
- $body='';$prefix='';$subfix='';
- $start=$curpage-($a_num-1)/2;//開始頁
- $end=$curpage+($a_num-1)/2; //結束頁
- $start=$start<=0?1:$start; //開始頁超范圍修正
- $end=$end>$pages?$pages:$end;//結束頁超范圍修正
- if($pages>=$a_num){//總頁數大于顯示頁數
- if($curpage<=($a_num-1)/2){$end=$a_num;}//當前頁在左半邊補右邊
- if($end-$curpage<=($a_num-1)/2){$start-=5-($end-$curpage);}//當前頁在右半邊補左邊
- }
- for($i=$start;$i<=$end;$i++){
- if($i==$curpage){
- $body.='<b>'.$i.'</b>';
- }else{
- $body.='<a href="'.str_replace('{page}',$i,$url).'"> '.$i.' </a>';
- }
- }
- $prefix=($curpage==1?'':'<a href="'.str_replace('{page}',1,$url).'">'.$first.'</a><a href="'.str_replace('{page}',$curpage-1,$url).'">'.$pre.'</a>');
- $subfix=($curpage==$pages?'':'<a href="'.str_replace('{page}',$curpage+1,$url).'">'.$next.'</a><a href="'.str_replace('{page}',$pages,$url).'">'.$last.'</a>');
- $info=" 第{$curpage}/{$pages}頁 ";
- $go='<script>function ekup(){if(event.keyCode==13){clkyup();}}function clkyup(){if(!//d+/.test(document.getElementById(/'gsd09fhas9d/').value)){alert(/'請輸入頁碼!/');return;};location=/''.$url.'/'.replace(///{page//}/,document.getElementById(/'gsd09fhas9d/').value);}</script><input onkeyup="ekup()" type="text" id="gsd09fhas9d" style="width:40px;vertical-align:text-baseline;padding:0 2px;font-size:10px;border:1px solid gray;"/> <span id="gsd09fhas9daa" onclick="clkyup();" style="cursor:pointer;text-decoration:underline;">轉到</span>';
- return $prefix.$body.$subfix.$info.$go;
- }
- /**
- * 說明:獲取 _SERVER['REQUEST_URI'] 值的通用解決方案
- */
- function request_uri()
- {
- if (isset($_SERVER['REQUEST_URI']))
- {
- $uri = $_SERVER['REQUEST_URI'];
- }
- else
- {
- if (isset($_SERVER['argv']))
- {
- $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];
- }
- else
- {
- $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
- }
- }
- return $uri;
- }
- /**
- * 獲取HTTP傳遞的整數
- * @param string $key 參數名稱
- * @param bool $ispost 采取POST傳遞方式,默認為真http://blog.ddian.cn
- * @return 整數,非法時默認為 $default
- */
- function httpInt($key, $ispost=true,$default=0) {
- if ($ispost) {
- if (isset($_POST[$key])) {
- if (empty($_POST[$key])) {
- return $default;
- } else {
- $val = trim($_POST[$key]);
- if (preg_match("/^/-?/d+$/", $val)) return intval($val);
- else return $default;
- }
- } else {
- return $default;
- }
- } else {
- if (isset($_GET[$key])) {
- if (empty($_GET[$key])) {
- return $default;
- } else {
- $val = trim($_GET[$key]);
- if (preg_match("/^/-?/d+$/", $val)) {
- return intval($val);
- } else {
- return $default;
- }
- }
- } else {
- return $default;
- }
- }
- }
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答