PHP仿Google分頁效果的函數執行個體

來源:互聯網
上載者:User
本文主要介紹了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 分頁(limit)

php 分頁原理詳解

Wordpress php 分頁代碼_PHP教程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.