<?php
/*
* Created on 2014-10-31
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-Type: text/html;charset=utf-8");
//连接数据库
$conn = @ mysql_connect("localhost:3306", 'root', 'root') or die("数据库服务器连接失败!");
mysql_select_db("phpstu") or die("数据库连接失败");
mysql_set_charset("utf8"); //设置当前连接的默认字符集。
//总记录数
$totalRecord = count(getData("select * from user"));
//当前页
if ($_GET and array_key_exists("page", $_GET) and (int) $_GET["page"] >= 1) {
$currentPage = $_GET["page"] ? (int) $_GET["page"] : 1;
} else {
$currentPage = 1;
}
//每页记录数
$pageSize = 5;
//总页数
$totalPage = ceil($totalRecord / $pageSize);
//组装获取当前页记录的SQL语句
$sql = "select * from user limit " . ($currentPage -1) * $pageSize . "," . $pageSize;
//存储当前页记录的数组
$rows = getData($sql);
//当前页的路径
$url = $_SERVER['PHP_SELF'];
//获取数据库中的数据
function getData($sql) {
$result = @ mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
$rows = array ();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP分页实例</title>
<style type="text/css">
table.pageTable{
width:500px;
margin:0 auto;
text-align:center;
font-family: verdana,arial,sans-serif;
font-size:12px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.pageTable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.pageTable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<table class="pageTable">
<tr><th>ID</th><th>用户名</th><th>密码</th></tr>
<?foreach($rows as $row):?>
<tr><td><?=$row["uid"];?></td><td><?=$row["uname"];?></td><td><?=$row["upassword"];?></td></tr>
<?endforeach;?>
<?if($totalRecord>$pageSize):?>
<tr><td colspan="3">
<? if ($currentPage == 1):?>
首页 上一页 <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?elseif($currentPage == $totalPage): ?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> 下一页 尾页
<?else:?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?endif;?>
</td></tr>
<? endif; ?>
</table>
</body>
</html>
/*
* Created on 2014-10-31
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-Type: text/html;charset=utf-8");
//连接数据库
$conn = @ mysql_connect("localhost:3306", 'root', 'root') or die("数据库服务器连接失败!");
mysql_select_db("phpstu") or die("数据库连接失败");
mysql_set_charset("utf8"); //设置当前连接的默认字符集。
//总记录数
$totalRecord = count(getData("select * from user"));
//当前页
if ($_GET and array_key_exists("page", $_GET) and (int) $_GET["page"] >= 1) {
$currentPage = $_GET["page"] ? (int) $_GET["page"] : 1;
} else {
$currentPage = 1;
}
//每页记录数
$pageSize = 5;
//总页数
$totalPage = ceil($totalRecord / $pageSize);
//组装获取当前页记录的SQL语句
$sql = "select * from user limit " . ($currentPage -1) * $pageSize . "," . $pageSize;
//存储当前页记录的数组
$rows = getData($sql);
//当前页的路径
$url = $_SERVER['PHP_SELF'];
//获取数据库中的数据
function getData($sql) {
$result = @ mysql_query($sql) or die("SQL语句出错,请检查SQL语句!");
$rows = array ();
while ($row = mysql_fetch_array($result)) {
$rows[] = $row;
}
return $rows;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP分页实例</title>
<style type="text/css">
table.pageTable{
width:500px;
margin:0 auto;
text-align:center;
font-family: verdana,arial,sans-serif;
font-size:12px;
color:#333333;
border-width: 1px;
border-color: #666666;
border-collapse: collapse;
}
table.pageTable th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #dedede;
}
table.pageTable td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #666666;
background-color: #ffffff;
}
</style>
</head>
<body>
<table class="pageTable">
<tr><th>ID</th><th>用户名</th><th>密码</th></tr>
<?foreach($rows as $row):?>
<tr><td><?=$row["uid"];?></td><td><?=$row["uname"];?></td><td><?=$row["upassword"];?></td></tr>
<?endforeach;?>
<?if($totalRecord>$pageSize):?>
<tr><td colspan="3">
<? if ($currentPage == 1):?>
首页 上一页 <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?elseif($currentPage == $totalPage): ?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> 下一页 尾页
<?else:?>
<a href="<?=$url?>?page=1">首页</a> <a href="<?=$url?>?page=<?=$currentPage-1?>">上一页</a> <a href="<?=$url?>?page=<?=$currentPage+1?>">下一页</a> <a href="<?=$url?>?page=<?=$totalPage?>">尾页</a>
<?endif;?>
</td></tr>
<? endif; ?>
</table>
</body>
</html>