欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP分页 upgrade 20130125

程序员文章站 2022-04-27 09:26:40
...
PHP分页 upgrade 20130125

分页用Class 这也太浪费资源了吧
@ 20130125
multi($total, $limit, $displayTab, $uri = NULL);
@parameter
$total 记录总数
$limit 单页记录数
$displayTab 定位按钮个数

<!doctype html> 
<html> 
<head> 
<meta charset="utf-8" /> 
<title>multi</title> 
<style> 
a, input, select, textarea { outline: medium none; } 
a:hover { background-color: #E5EDF2; } 
.current { background-color: #E5EDF2; 
border: 1px solid #C2D5E3; 
color: #000000; display: block; float: left; font-family: Tahoma; 
font-size: 13px; font-weight: bold; line-height: 26px; margin-left: 5px; padding: 0 10px; 
text-align: center; text-decoration: none; } 
.common { border: 1px solid #C2D5E3; color: #000000; display: block; float: left; 
font-family: Tahoma; font-size: 13px; line-height: 26px; margin-left: 5px; padding: 0 10px; 
text-align: center; text-decoration: none; } 
.jump { border: 1px solid #C2D5E3; color: #000000; display: block; float: left;
 font-family: Tahoma; font-size: 13px; line-height: 26px; margin-left: 5px; padding: 0 10px;
  text-align: center; text-decoration: none; } 
</style> 
</head> 
  
<body> 
<?php 
 echo multi(1000, 10, 10); 
 function multi($total, $limit, $displayTab, $uri = NULL) { 
    if($total <= $limit || $total == $limit) return (bool)false; 
    $retval = ''; 
    $maxRange = $range = ceil($total/$limit); 
    $displayTab = $maxRange < $displayTab ? $maxRange : $displayTab; 
    $page = !empty($_GET['page']) && is_numeric($_GET['page']) ?  
    (intval($_GET['page']) > 0 && intval($_GET['page']) <= $maxRange ?  
    intval($_GET['page']) :  
    (intval($_GET['page']) > $maxRange ? $maxRange : 1)) : 1; 
    $previous = $uri ? $uri.($page-1 <= 0 ? 1 : ($page-1 >= $maxRange ? $maxRange-1 :
     $page-1 )) : 
    $_SERVER['PHP_SELF'].'?page='.($page-1 <= 0 ? 1 : ($page-1 >= $maxRange ? $maxRange-1 :
    $page-1 )); 
    $next = $uri ? $uri.($page+1 > $maxRange ? $maxRange : $page+1) : $_SERVER['PHP_SELF'].
    '?page='.(
    $page+1 > $maxRange ? $maxRange : $page+1); 
    $jump = $uri ? $uri : $_SERVER['PHP_SELF'].'?page='; 
    $retval .= $page > 1 ? '<a class="common" href="'.$previous.'" >上一页</a>' : ''; 
    if(empty($_GET['page']) || $_GET['page'] < $displayTab) { 
        $start = 0; 
        $range = $displayTab; 
    }else { 
        $range = !empty($_GET['page']) && intval($_GET['page']) + ($displayTab/2)  >= $range ? 
        $range : intval($_GET['page']) + ($displayTab/2); 
        $start = $range - $displayTab + 1; 
    } 
    for($i= $start; $i <= $range; $i++) { 
            if($i <= 0) continue; 
  $retval .= ((!empty($_GET['page']) && is_numeric($_GET['page']) ? ($_GET['page'] > $maxRange ?
    $maxRange : (intval($_GET['page']) <= 0 ? 1 : intval($_GET['page']))) : 1 ) == $i) ? '
           <a class="current" href="'. $jump.$i .'">'. $i .'</a>' :
             '<a class="common" href="'. $jump.$i .'">'. $i .'</a>'; 
    } 
    $retval .= ($maxRange - (!empty($_GET['page']) && is_numeric($_GET['page']) ?
     $_GET['page'] : 1 )) >
     $displayTab ? '<a class="common" href="'. $jump.$maxRange .'">...'. $maxRange .'</a>' : ''; 
    $retval .= (!empty($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1 ) 
    < $maxRange ? '<div class="jump">
<input id="code" type="text" name="page" size="3" title="输入页码,按回车快速跳转"
 onkeyup="submits(event);">/ '. $maxRange .'页</div>' : ''; 
    $retval .= (!empty($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 1 ) 
    < $maxRange ? '<a class="common" href="'. $next .'">下一页</a>' : ''; 
    $retval .= ' 
    <script type="text/javascript"> 
    function submits(e) { 
        var code = document.getElementById(\'code\'); 
        if(code.value != \'\') { 
            if(code.value.match(/[0-9]{1,}/)) { 
                if(e.keyCode == 13) { 
                    location.href= \''.$jump.'\' + code.value; 
                } 
            }else{ 
                if(e.keyCode == 13) { 
                    code.value = \'\'; 
                } 
            } 
        } 
    } 
    </script>'; 
    return $retval; 
} 
  
?> 
</body> 
</html>

2. [代码]20111227第一版

<?php  
/**  
*      [WebodMin] (C)2010-2099 Blank Inc.  
*      This is open source code, You can borrow, but can not modify the copyright  
*      Update: 2011-12-27 
**/ 
function multi($num,$limit,$loopvar,$style,$str=''){     
$count = ceil($num/$limit);     
$loopvar = $count < $loopvar ? $count : $loopvar;     
$_GET['page'] = !empty($_GET['page']) && $_GET['page'] <= $count ? $_GET['page'] : 1;     
$str .= $_GET['page'] > 1 && $_GET['page'] <= $count ? '<a class="'.$style.'" 
href="?page='.($_GET['page']-1 <=0 ? 1 : $_GET['page']-1).'">上一页</a>' : $_GET['page'] > 1 ? 
'<a class="'.$style.'" href="?page='.($_GET['page']-1 <=0 ? 1 : $_GET['page']-1).'">上一页</a>' : false;     
if(empty($_GET['page']) || $_GET['page'] < $loopvar) {         
for($i=0;$i<=$loopvar;$i++) {             
if($i !=0 ) {                 
$str.= $_GET['page'] == $i ? '<a class="cruuent" href="?page='.$i.'">'.$i.'</a>' :
 '<a class="'.$style.'" href="?page='.$i.'">'.$i.'</a>';               
 }        
  }     
  }
  else{        
   $size = $_GET['page']+1 >= $count ? $count : $_GET['page']+1;          
   $page = ($size-$loopvar)+1;         
   for($i=$page;$i<=$size;$i++) {             
   if($i !=0 ) {                 
   $str.= $_GET['page'] == $i ? '<a class="cruuent" href="?page='.$i.'">'.$i.'</a>' : '
   <a class="'.$style.'" href="?page='.$i.'">'.$i.'</a>';               
   }         
   }     
   }     
   $str .= $_GET['page'] < $count ? '<a class="'.$style.'" href="?page='.($_GET['page']+1 >=$count ?
    $count : $_GET['page']+1).'">下一页</a>' : false;     
    $str .='<a class="countNumber">共:'.$count.'页</a>';     
    return $str; }  echo multi(150,10,5,'a'); ?> <style> 
    .a {     
    text-align: center;    
     display: block;     
     padding: 5px 10px 5px 10px;    
      margin-right: 5px;    
       float: left;    
        font-size: 13px;    
         color: #000;     
         text-decoration: none;     
         border: 1px solid #c2d5e3;     
         font-family: Tahoma;
          } 
          a:hover {     
          border: 1px solid #000;    
           font-weight:bold; } 
           a.cruuent {     
           text-align: center;     
           display: block;     
           padding: 5px 10px 5px 10px;    
            margin-right: 5px;    
             float: left;     
             font-size: 13px;     
             color: #000;     
             text-decoration: none;    
              border: 1px solid #c2d5e3;    
               font-family: Tahoma;     
               background-color:#e5edf2;     
               font-weight:bold; }
                a.countNumber {    
                 border:none;    
                  text-align: left;    
                   display: block;    
                    padding:5px 5px 0px 5px;    
                     margin-right: 5px;    
                     float: left;     
                     font-size: 13px;     
                     font-weight:bold;    
                      color: #333;     
                      text-decoration: none;    
                       font-family: Tahoma; } 
                       </style>

以上就是PHP分页 upgrade 20130125的内容,更多相关内容请关注PHP中文网(www.php.cn)!