dd

PHP分页如何实现上一页与下一页功能的?

jerry PHP 2015年08月28日 收藏
/**
 * 产生分页链接 
 *
 * @param Integer $intSum 
 * @param Integer $intPage
 * @param Integer $intPerPage
 * @param String $strURI
 * @return String
 *
 * @Example: $strMultipage = multi(121, $_GET['page'], 20, 'index.php');
 * @Example: $strMultipage = multi(121, $_GET['page'], 20, 'index.php?type=2');
 *
 */
function multi($intSum, $intPage, $intPerPage, $strURI)
{
 if ($intSum <= $intPerPage || $intPerPage == 0) {
  return '';
 }
 $strURI .= strpos($strURI, '?') ? '&' : '?';
 $intPages = ceil($intSum / $intPerPage);
 $intPageNum = 10;
 $intOffset = floor($intPageNum / 2);
 $intMinPage = ($intPage < $intOffset) ? 1 : $intPage - $intOffset + 1;
 $intMinPage = ($intPage + $intOffset > $intPages) ? $intPages - $intPageNum + 1: $intMinPage;
 $intMinPage = $intMinPage > 0 ? $intMinPage : 1;
 $intMaxPage = $intMinPage + $intPageNum - 1;
 $intMaxPage = $intMaxPage <= $intPages ? $intMaxPage : $intPages;
 
 $strPages = '';
 for($i = $intMinPage; $i <= $intMaxPage; $i++) {
  if ($i == $intPage) {
   $strPages .= ' <b>[ ' . $i . ' ]</b>';
  } else {
   $strPages .= ' <a href="' .  $strURI . 'page=' . $i . '" class="blueLink">[ ' . $i . ' ]</a>';
  }
 }
 if ($intPage != 1) {
  $strPages = '<a href="' . $strURI . 'page=1" class="blueLink">首页</a> <a href="' . $strURI . 'page=' . ($intPage - 1) . '" class="blueLink">上一页</a>' . $strPages;
 } else {
  $strPages = '首页 上一页' . $strPages;
 }
 if ($intPage != $intPages) {
  $strPages .= ' <a href="' . $strURI . 'page=' . ($intPage + 1) . '" class="blueLink">下一页</a> <a href="' . $strURI . 'page=' . $intPages . '" class="blueLink">末页</a>';
 } else {
  $strPages .= ' 下一页 末页';
 }
  
 return $strPages;
}

完整实例:

<?php  
// 建立数据库连接  
$link = mysql_connect("localhost", "root", "wyh7ye") ; 
;// 获取当前页数  
mysql_select_db("test",$link); 
if(isset($_GET[’page’]))...{  
$page = intval($_GET[’page’]);  
}  
else...{  
$page = 1;  
}  
// 每页数量  
$page_size =4;  
// 获取总数据量  
$sql = "select * from user";  
$result = mysql_query($sql,$link);  
while($row = mysql_fetch_array($result))...{ 
$i=$i+1; 
} 
$amount = $i;  
// 记算总共有多少页  
if( $amount )...{  
    if( $amount < $page_size )...{ $page_count = 1; } //如果总数据量小于$PageSize,那么只有一页  
    if( $amount % $page_size )...{ //取总数据量除以每页数的余数  
        $page_count = (int)($amount / $page_size) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一  
    }else...{  
        $page_count = $amount / $page_size ; //如果没有余数,则页数等于总数据量除以每页数的结果  
    }  
}  
else...{  
$page_count = 0;  
}  
// 获取数据,以二维数组格式返回结果  
if( $amount )...{  
$sql = "select * from user order by id desc limit ". ($page-1)*$page_size .",$page_size";  
$result =mysql_query($sql,$link);  
while ($row =mysql_fetch_array($result))...{  
?> 
<table width="100%"  border="0" cellspacing="2" cellpadding="0"> 
  <tr> 
    <td bgcolor="#CCCCCC" width="25%"><?php echo $row[0];?></td> 
    <td bgcolor="#FFCCFF" width="25%"><?php echo $row[1];?></td> 
    <td bgcolor="#FFFFCC" width="25%"><?php echo $row[2];?></td> 
    <td bgcolor="#CCCCCC" width="25%"><?php echo $row[3];?></td> 
  </tr> 
</table> 
<?php 
}  
} 
// 翻页链接  
$page_string ="";  
if( $page == 1 )...{  
$page_string.="第一页|上一页|";  
}  
else...{  
$page_string.= "<a href=?page=1>第一页</a>|<a  
href=?page=".($page-1).">上一页</a>|";  
}  
if( ($page == $page_count) || ($page_count == 0) )...{  
$page_string.=" 下一页|尾页";  
}  
else...{  
$page_string.= "<a href=?page=".($page+1).">下一页</a>|<a href=?page=".$page_count.">尾页</a>"; 
}  
echo $page_string; 
?>
dd