PHP分页

<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值