公共文件common.php
<?php
header("content-type:text/html;charset=utf-8");
//配置信息
$db_host='localhost'; //主機名
$db_port='3306'; //端口號
$db_user='root'; //用戶名
$db_pass='123456'; //密碼
$db_name='students'; //數(shù)據(jù)庫名稱
$charset='utf8'; //字符集
//php連接mysql服務(wù)器
$link=@mysql_connect($db_host.':'.$db_port,$db_user,$db_pass);
if(!$link)
die('php數(shù)據(jù)庫連接失敗'.mysql_error());
//選擇當前數(shù)據(jù)庫
if(!mysql_select_db($db_name))
die('選擇數(shù)據(jù)庫失敗'.mysql_error());
//設(shè)置客戶端字符集
mysql_query("set charset {$charset}");
list.php分頁文件
<?php
//包含連接數(shù)據(jù)庫的公共文件
require_once("./conn.php");
//每頁顯示數(shù)
$pagesize=5;
//獲取當前頁碼和計算開始行號
$page=isset($_GET['page']) ? $_GET['page']:1;
$startrow=($page-1)*$pagesize;
//計算總記錄數(shù)和總頁數(shù)
$sql="select*from person";
$result=mysql_query($sql);
$records=mysql_num_rows($result);
$pages=ceil($records / $pagesize);
//構(gòu)建分頁的sql語句
$sql .=" order by id asc limit {$startrow},{$pagesize}";
//執(zhí)行sql語句,并返回結(jié)果集
$result=mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>學生信息管理中心</title>
<style type="text/css">
.pagelist a,.pagelist span{
display: inline-block;
padding:5px 10px;
border:1px solid #ddd;
margin:0 3px;
text-decoration: none;
}
.pagelist span{color:red;border-color:red;}
</style>
</head>
<body>
<div class="content" style="text-align: center;margin-bottom:20px;">
<h2>學生信息管理中心</h2>
</div>
<table width='600' border='1' bordercolor='#ccc' align='center' cellpadding='5' cellspacing='0'>
<tr bgcolor="#f0f0f0">
<th>編號</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>學歷</th>
<th>愛好</th>
<th>工資</th>
<th>獎金</th>
<th>籍貫</th>
</tr>
<?php
while($arr=mysql_fetch_assoc($result)){
?>
<tr>
<td><?php echo $arr['id'] ?></td>
<td><?php echo $arr['name'] ?></td>
<td><?php echo $arr['age'] ?></td>
<td><?php echo $arr['sex'] ?></td>
<td><?php echo $arr['edu'] ?></td>
<td><?php echo $arr['hobby'] ?></td>
<td><?php echo $arr['salary'] ?></td>
<td><?php echo $arr['bonus'] ?></td>
<td><?php echo $arr['city'] ?></td>
</tr>
<?php } ?>
<tr>
<td colspan="9" align="center" class='pagelist'>
<?php
//計算循環(huán)的起點和終止頁
$start=$page-1;
$end=$page+1;
if($page<2){
$start=1;
$end=3;
}
if($page>$pages-1){
$start=$pages-2;
$end=$pages;
}
for ($i=$start; $i <=$end; $i++) {
//如果是當前頁,不加鏈接
if($i==$page){
echo "<span>$i</span>";
}else{
echo "<a href='?page=$i'>$i</a>";
}
}
?>
</td>
</tr>
</table>
</body>
</html>

分頁信息表1.png

分頁信息表2.png