qq_責责_0
2017-08-12 15:29
都能运行但是每一页里边的数据不一样多是因为什么?是哪里有写错的地方吗?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<style type="text/css">
body {font-size-12px;
}
div.page {
text-align: center
}
div.content {
height: 300px;
}
div.page a {
border: 1px solid;
text-decoration: none;
padding: 2px 5px 2px 5px;
margin: 2px;
}
div.page span.current {
border: 1px soild #black;
background-color: #000099;
padding: 4px 6px 4px 6px;
margin: 2px;
color: #fff;
}
div.page span.disable {
border: 1px soild #blue;
padding: 2px 5px 2px 5px;
margin: 2px;
color: #ddd;
}
div.page form {
display: inline;
}
</style>
<body>
<?php
// header("Content-type:text/html;charset=utf-8");
// 传入页码
if(empty($_GET['p'])){
$page = 1;
}else{
$page = $_GET['p'];
}
//$page = $_GET['p'];
// 根据页码取出数据
$pagesize = 6;//
$showpage = 5;
// 连接数据库
$conn = mysql_connect("localhost","root", "root");
if (! $conn) {
echo "数据库链接失败";
exit();
}
// 选择所要操作的数据库
mysql_select_db("test",$conn);
// 设置数据库编码格式
mysql_query("SET NAMES UTF8");
// 编写sql获取分页数据 select * from 表名 limit起始位置 显示条数
$sql = "SELECT*FROM page LIMIT " . (($page - 1) * $pagesize) . ",6";
// 把sql语句传入数据
$result = mysql_query($sql);
// 处理数据
echo "<div class='content'>";
echo "<table border=1 cellspacing=0 width=40% align=center>";
echo "<tr><td>username</td><td>password</td></tr>";
while ($row = mysql_fetch_assoc($result)) {
// echo $row['id'].'-'.$row['name'].'<br>';
echo "<tr>";
echo "<td>{$row['username']}</td>";
echo "<td>{$row['password']}</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
// 获取数据总条数
$total_sql = "SELECT COUNT(*) FROM page";
$total_result = mysql_fetch_array(mysql_query($total_sql));
$total = $total_result[0];
// 计算页数
$total_pages = ceil($total / $pagesize);
//exit();
// 释放结果,关闭链接
mysql_free_result($result);
mysql_close($conn);
// 显示数据+分页条
$page_banner = "<div class='page'>";
// 显示偏移量
$pageoffset = ($showpage - 1) / 2;
// 初始化数据
$start = 1;
$end = $total_pages;
if ($page > 1) {
$page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=1'>首页</a>";
$page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($page - 1) . "'><上一页</a>";
} else {
$page_banner .= "<span class='disable'>首页</a></span>";
$page_banner .= "<span class='disable'>上一页</a></span>";
}
if ($total_pages > $showpage) {
if ($page > $pageoffset + 1) {
$page_banner .= "...";
}
if ($page > $pageoffset) {
$start = $page - $pageoffset;
$end = $total_pages > $page + $pageoffset ? $page + $pageoffset : $total_pages;
} else {
$start = 1;
$end = $total_pages > $showpage ? $showpage : $total_pages;
}
if ($page + $pageoffset > $total_pages) {
$start = $start - ($page + $pageoffset - $end);
}
}
for ($i = $start; $i <= $end; $i ++) {
if ($page == $i) {
$page_banner .= "<span class='current'>{$i}</span>";
} else {
$page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . $i . "'>{$i}</a>";
}
}
//尾部省略
if ($total_pages > $showpage && $total_pages > $page + $pageoffset) {
$page_banner .= "...";
}
if ($page < $total_pages) {
$page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($page + 1) . "'>下一页></a>";
$page_banner .= "<a href='" . $_SERVER['PHP_SELF'] . "?p=" . ($total_pages) . "'>尾页</a>";
} else {
$page_banner .= "<span class='disable'>下一页</a></span>";
$page_banner .= "<span class='disable'>尾页</a></span>";
}
$page_banner .= "共{$total_pages}页";
$page_banner .= "<form action='select.php' method='get'>";
$page_banner .= "到第<input type='text' size='2' name='p'>页";
$page_banner .= "<input type='submit' value='确定'>";
$page_banner .= "</form></div>";
echo $page_banner;
?></body>
</html>
可能是数据库查询语句错了
PHP+MySQL分页原理实现
41180 学习 · 175 问题
相似问题