Demo entry 6354803

d

   

Submitted by d on Apr 11, 2017 at 07:17
Language: PHP. Code size: 3.0 kB.

<?php function page($count,$page_size,$num_btn=10,$page='page'){
    //判断
    
    if(!isset($_GET['page'])||!is_numeric($_GET['page'])||$_GET['page']<1){
        $_GET[$page]=1;
        
    }
   
   $page_num_all = ceil($count/$page_size);
   if($_GET[$page]>$page_num_all){
       $_GET[$page]=$page_num_all;
   }
   $start=($_GET[$page]-1)*$page_size;
   $limit="limit {$start},{$page_size}";
   
   $current_url=$_SERVER['REQUEST_URI'];
   $arr_current=parse_url($current_url);//url拆分到数组里面
   $current_path=$arr_current['path'];
   if(isset($arr_current['query'])){
       parse_str($arr_current['query'],$arr_query);
       unset($arr_query[$page]);
       if(empty($arr_query)){
           $url="{$current_path}?{$page}=";
       }else{
           $other=http_build_query($arr_query);
           $url="{$current_path}?{$other}&{$page}=";
       }
   }else{
       $url="{$current_path}?{$page}=";
   }
   $html=array();
   if($num_btn>=$page_num_all){
       //显示所有按钮
       for($i=1;$i<=$page_num_all;$i++){
           //$i 限制循环次数,也是页码号
           if($_GET[$page]==$i){
              $html[$i].="<li class='active'><span >{$i}</span></li> ";
           }else{
              $html[$i].="<li><a href='{$url}{$i}'>{$i}</a></li> ";
           }
           
       }
   }else{
       //考虑左边和右边越界的情况
       $num_left= floor(($num_btn-1)/2);
       $start=$_GET[$page]- $num_left;
       $end=$start+$num_btn-1;
       if ($start<1){
           $start=1;
       }
       if($end>$page_num_all){
           $start=$page_num_all-$num_btn+1;
       }
       for($i=0;$i<$num_btn;$i++){
           if($_GET[$page]==$start){
           $html[$start].="<li class='active'><span >{$start}</span></li>";
           }else{          
           $html[$start].="<li><a href='{$url}{$start}'>{$start}</a></li>";      
           }
           $start++;
       }
       //如果按钮数目大于3的时候做省略号
       if(count($html)>=3){
           reset($html);
           $key_first=key($html);
           end($html);
           $key_end=key($html);
           if($key_first!=1){
               array_shift($html);
               array_unshift($html, "<li><a href='{$url}1'>1...</a></li>");
              
           }
           if($key_end!=$page_num_all){
                array_pop($html);
                array_push($html, "<li><a href='{$url}{$page_num_all}'>...{$page_num_all}</a></li>");
           }
       }
      
   }
   //把数组生成字符串
   if($_GET[$page]!=1){
       $next=$_GET[$page]-1;
       array_unshift($html, "<li><a href='{$url}{$next}'>&laquo;</a></li>");
   }
   if($_GET[$page]!=$page_num_all){
       $next=$_GET[$page]+1;
       array_push($html, "<li><a href='{$url}{$next}'> &raquo;</a></li>");
   }
   array_unshift($html, '<nav><ul class="pagination">');
   array_push($html, '</ul><nav>');
   $html=implode(' ', $html);
  
   $data=array(
       'limit'=>$limit,
       'html'=>$html
   );
   return $data;

}?>

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).