php如何分页显示数据库中的数据,主要是建立连接,读取数据行数,设置pagecount数,设置每页显示的数据行数,进行计算,用表格嵌套php来实现输出。
代码如下
<html>
<head><title>分页显示</title>
<meta http-equiv="Content-Type" content="text/html" ;charset=utf-8" />
</head>
<body>
<?php
//$page=$_GET['page'];
$page=$_GET['page'];
if($page ==0)
$page = 1;
$PageSize=2;
$conn= mysqli_connect("localhost","root","root","datevase");
if(empty($conn))
die("mysqli_connect failed: ".musqli_connect_error());
$sql="SELECT COUNT(1) FROM userInformation";
$results=$conn->query($sql);
$row = $results->fetch_row();
$RecordCount=$row[0];
if($RecordCount)
{
if($RecordCount<$PageSize)
$PageSize=1;
if($RecordCount % $PageSize)
{
$PageCount = (int )($RecordCount % $PageSize +1);
}
else
$PageCount=$RecordCount % $PageSize;
}
else
$PageCount=0;
////////
//set language
///////
mysqli_query($conn,"SET NAMES gb2312");
echo("<br>当前页数:". $page."/".$PageCount);
?>
<table width ="500" border="1">
<tr>
<td>UserName</td>
<td>UserId</td>
<td>UserSex</td>
<td>座右铭</td>
<td>E-Mail</td>
<td>密码</td>
<td>注册日期</td>
<td>Other</td>
</tr>
<?php
//循环显示内容
$sql=("SELECT userName,userID,userSex,userMoto,userMail,userPassword,userRegisterData,userProMenuName
FROM userInformation LIMIT ".($page-1)*$PageSize.",".$PageSize);
$results=$conn->query($sql);
while($row=$results->fetch_row())
{
echo("<tr>");
echo ("<td>". $row[0]." </td>");
echo ("<td>". $row[1]." </td>");
echo ("<td>". $row[2]." </td>");
echo ("<td>". $row[3]." </td>");
echo ("<td>". $row[4]." </td>");
echo ("<td>". $row[5]." </td>");
echo ("<td>". $row[6]." </td>");
echo ("<td>". $row[7]." </td>");
echo ("</tr>");
}
mysqli_close($conn);
//显示分页连接
if( $page == 1)
echo("第一页");
else
echo(" <a href=pageShow.php?page=1>第一页</a>");
//设置"上一页"连接
if($page==1)
echo ("上一页");
else
echo (" <a href=pageShow.php?page=". ($page-1 ).">上一页</a>");
if($page=$PageCount)
echo ("下一页");
else
echo (" <a href=pageShow.php?page=" . ($page+1) .">下一页</a>");
if($page=$PageCount)
echo ("最后一页");
else
echo (" <a href=pageShow.php?page=" . $PageCount . ">最后一页</a>");
?>
</table>
</body>
</html>