復制代碼 代碼如下:
<?php
//登陸頁面設置的session,當session中存在name時
//session_start();
//$name = $_SESSION['name'];
//if (empty($name)){
// header("Location: error.php");
// exit();
//}
//面向過程, 數據分頁顯示
if(false!=($mysql = mysql_connect('本地mysql', 'mysql用戶名', 'mysql密碼'))){
mysql_query('set names utf8',$mysql); //設置數據庫中的編碼
mysql_select_db("數據庫database",$mysql);
}else{
die("連接失敗");
}
$pageSize = 10; //頁面顯示條數
$rowCount = 0; //數據總條數,從數據庫獲得
$sqlCount = 'select count(id) from employee';
$res1 = mysql_query($sqlCount,$mysql);
//取出數據條數
if(false!=($row=mysql_fetch_row($res1))){
$rowCount = $row[0];
}
//總頁數,通過計算得到
$pageCount = 0;
$pageCount = ceil($rowCount/$pageSize);
//獲取當前頁
if(!isset($_GET['pageNow'])){ // 當 get/post都為空的時候賦默認值1
$pageNow = 1; //當前頁數
}elseif(false!=is_numeric($_GET['pageNow']) && $_GET['pageNow']<=$pageCount){
$pageNow = $_GET['pageNow'];
}else{
header("Location: ../Error/error.php");
exit();
}
//打印分頁數據
echo "<div style='margin-left:300px;margin-top:1px;'>";
echo "<table style='border:1px;border-style:solid;border-width:1px;border-color:green'>";
echo "<tr><th>id</th> <th>name</th> <th>age</th> <th>sex</th> <th>birthday</th> <th>編輯雇員</th> <th>刪除雇員</th> </tr>";
$sqList = "select id,name,age,sex,birthday from employee limit ".($pageNow-1)*$pageSize.",".$pageSize;
$res2 = mysql_query($sqList,$mysql);
while (false!=($row=mysql_fetch_assoc($res2))){
echo "<tr><td>{$row['id']}</td> <td>{$row['name']}</td> <td>{$row['age']}</td> <td>{$row['sex']}</td> <td>{$row['birthday']}</td> <td><a href=#>編輯</a></td> <td><a href=#>刪除</a></td></tr>";
}
echo "</table>";
//表單控制顯示頁數
echo "<form action='當前頁'>";
//上一頁按鈕
if($pageNow>1){
$pageUp = $pageNow-1;
echo "<a href='?pageNow=".$pageUp."'>上一頁</a> ";
}
//下一頁按鈕
if($pageNow<$pageCount){
$pageDown = $pageNow+1;
echo "<a href='?pageNow=".$pageDown."'>下一頁</a> <br/>";
}
//後退十頁按鈕
if($pageNow-10>0){
echo "<a href='?pageNow=".($pageNow-10)."'><<<</a> ";
}
//向本頁傳遞當前顯示的頁數,並顯示第幾頁按鈕
for($i=1;$i<=$pageCount;$i++){
if($i>$pageNow-2 && $i<$pageNow+6){
if($i!=$pageNow){
echo "<a href='?pageNow=".$i."'>第".$i."頁</a> ";
}
}
}
//前進十頁
if($pageNow+10<=$pageCount){
echo "<a href='?pageNow=".($pageNow+10)."'>>>></a> ";
}
//顯示當前頁與總頁數
echo "<br/>當前頁".$pageNow."頁/共".$pageCount."頁";
//跳轉頁
echo "跳轉到:<input type='text' name='pageNow' id='pageNow' style='width:30px;height:20px'/>頁<input type='submit' style='width:37px;height:20px;font-size:11px;' value='go'/>";
echo "</form>";
echo "</div>";
?>