// 获取数据总数
$total_sql = "SELECT COUNT(*) FROM users";
$total_result = mysql_fetch_array(mysql_query($total_sql)) ;
$total = $total_result[0];
// echo "总条数".$total;exit;
// 计算页数
$total_pages = ceil($total/10); //向上取一 向下取一 floor
mysql_close($conn);
//3 显示数据 + 分页条
$page_banner = "<div class='page'>";
//计算偏移量
$pageoffset = ($showPage-1)/2;
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'>首页</span>";
$page_banner .= "<span class='disable'><上一页</span>";
}
// 初始化数据
$start = 1;
$end = $total_pages;
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'>尾页</span>";
$page_banner .= "<span class='disable'>下一页></span>";
}
$page_banner .= "共{$total_pages}页,";
// 跳转页码
$page_banner .= "<form action='mypage.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";
?>