徐逸以轩
2016-05-24 16:33
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>分页</title> <meta name="keywords" content="分页设计"> <meta name="description" content="分页"> <meta name="author" content="慕课"> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <link rel="stylesheet" type="text/css" href="page.css"> </head> <body> <?php header("Content-Type:text/html;charset=UTF-8"); //1传入页码 $page=$_GET['p']; //2根据页码取出数据:php->mysql处理 $host="localhost"; $username="root"; $password=""; $db="imooc"; $pageSize=3;//每页显示条数 $showPage=5;//显示页码个数 //链接数据库 $conn=mysql_connect($host,$username,$password); if(!$conn){ echo "数据库链接失败"; exit; } //选择数据库 mysql_select_db($db); //设置数据库编码格式 mysql_query("set names utf8"); //编写sql获取分页数据 //$sql="select goods_id,goods_name,goods_price from tdb_goods order by goods_id limit" .($page-1)*10. ",10"; $sql="select goods_id,goods_name,goods_price from tdb_goods order by goods_id limit ".(($page-1)*$pageSize).",{$pageSize}"; //把sql语句传到数据库 $result=mysql_query($sql); //处理数据 echo "<div class='content'>"; echo "<table border=1 cellspacing=0 width=60% align=center>"; echo "<caption><h1>商品表</h1></caption>"; echo '<th>商品ID</th>'; echo '<th>商品名称</th>'; echo '<th>商品价格</th>'; echo '</tr>'; while($row=mysql_fetch_assoc($result)){ //echo $row["goods_id"].'-'.$row["goods_name"]."<br>"; echo "<tr>"; echo "<td>{$row['goods_id']}</td>"; echo "<td>{$row['goods_name']}</td>"; echo "<td>{$row['goods_price']}</td>"; echo "</tr>"; } echo "</table>"; echo "</div>"; //释放结果 关闭连接 mysql_free_result($result); //获取数据总数 $total_sql="select count(*) from tdb_goods"; $total_result=mysql_fetch_array(mysql_query($total_sql)); $total=$total_result[0]; /*$result=mysql_query("select * from tdb_goods"); $total=mysql_num_rows($result);*/ //计算页数 $total_page=ceil($total/$pageSize); //echo "总条数{$total}";exit; 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>"; } //初始化数据 $start=1;//哪一条开始显示页码 $end=$total_page; if($total_page>$showPage){ if($page>$pageoffset+1){ $page_banner.="..."; } if($page>$pageoffset){ $start=$page-$pageoffset; $end=$total_page>$page+$pageoffset?$page+$pageoffset:$total_page; }else{ $start=1; $end=$total_page>$showPage?$showPage:$total_page; } if($page+$pageoffset>$total_page){ $start=$start-($page+$pageoffset-$end); //$end=$total_page; } } for($i=$start;$i<=$end;$i++){ $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".$i."'>{$i}</a>"; } //尾部省略 if($total_page>$showPage&&$total_page>$page+$pageoffset){ $page_banner.="..."; } //$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>"; //$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>"; if($page<$total_page){ $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>"; $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p={$total_page}'>尾页</a>"; } $page_banner.="共{$total_page}页;"; $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; ?> </body> </html>
page.css:
div.page a{
border:#aaaadd 1px solid;
text-decoration:none;
}
我自己找到原因了 我晕 我第79行的$page_banner.= 少了一个拼接. 导致出错 。出现和我一样情况的 注意一下 $page_banner.=是不是没加.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>分页</title> <meta name="keywords" content="分页设计"> <meta name="description" content="分页"> <meta name="author" content="慕课"> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> <link rel="stylesheet" type="text/css" href="page.css"> </head> <body> <?php header("Content-Type:text/html;charset=UTF-8"); //1传入页码 $page=$_GET['p']; //2根据页码取出数据:php->mysql处理 $host="localhost"; $username="root"; $password=""; $db="imooc"; $pageSize=3;//每页显示条数 $showPage=5;//显示页码个数 //链接数据库 $conn=mysql_connect($host,$username,$password); if(!$conn){ echo "数据库链接失败"; exit; } //选择数据库 mysql_select_db($db); //设置数据库编码格式 mysql_query("set names utf8"); //编写sql获取分页数据 //$sql="select goods_id,goods_name,goods_price from tdb_goods order by goods_id limit" .($page-1)*10. ",10"; $sql="select goods_id,goods_name,goods_price from tdb_goods order by goods_id limit ".(($page-1)*$pageSize).",{$pageSize}"; //把sql语句传到数据库 $result=mysql_query($sql); //处理数据 echo "<div class='content'>"; echo "<table border=1 cellspacing=0 width=60% align=center>"; echo "<caption><h1>商品表</h1></caption>"; echo '<th>商品ID</th>'; echo '<th>商品名称</th>'; echo '<th>商品价格</th>'; echo '</tr>'; while($row=mysql_fetch_assoc($result)){ //echo $row["goods_id"].'-'.$row["goods_name"]."<br>"; echo "<tr>"; echo "<td>{$row['goods_id']}</td>"; echo "<td>{$row['goods_name']}</td>"; echo "<td>{$row['goods_price']}</td>"; echo "</tr>"; } echo "</table>"; echo "</div>"; //释放结果 关闭连接 mysql_free_result($result); //获取数据总数 $total_sql="select count(*) from tdb_goods"; $total_result=mysql_fetch_array(mysql_query($total_sql)); $total=$total_result[0]; /*$result=mysql_query("select * from tdb_goods"); $total=mysql_num_rows($result);*/ //计算页数 $total_page=ceil($total/$pageSize); //echo "总条数{$total}";exit; 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_page; if($total_page>$showPage){//总页数大于显示页数 如总页数就是5页 显示也是5页 if($page>$pageoffset+1){//如果当前页大于了偏移量(这里是2)+1,则前面的用...代替 $page_banner.="..."; } if($page>$pageoffset){//如果当前页大于了偏移量 $start=$page-$pageoffset; $end=$total_page>$page+$pageoffset?$page+$pageoffset:$total_page; }else{ $start=1; $end=$total_page>$showPage?$showPage:$total_page; } if($page+$pageoffset>$total_page){ $start=$start-($page+$pageoffset-$end); //$end=$total_page; } } 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_page>$showPage&&$total_page>$page+$pageoffset){ $page_banner.="..."; } //$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page-1)."'>上一页</a>"; //$page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页</a>"; if($page<$total_page){//如果是尾页 则不显示下一页尾页 $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p=".($page+1)."'>下一页></a>"; $page_banner.="<a href='".$_SERVER['PHP_SELF']."?p={$total_page}'>尾页</a>"; }else{ $page_banner.="<span class='disable'>下一页></span>"; $page_banner.="<span class='disable'>尾页</span>"; } $page_banner.="共{$total_page}页;"; $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>"; $page_banner.="</div>"; echo $page_banner; ?> </body> </html>
page.css:
body,html{ padding:0px; margin:0px;font-family:"宋体",Arial,Lucida,Verdana,Helvetica,sans-serif; font-size:12px;width:100%;}
h1,h2,h3,h4,h5,h6,ul,li,dl,dt,dd,form,img,p,label{margin:0; padding:0; border:none; list-style-type:none;}
div.page{
text-align:center;
}
div.content{
height:300px;
}
div.page a{
border:#aaaadd 1px solid;
padding:2px 5px 2px 5px;
margin:2px;
text-decoration:none;
}
div.page span.current{
border:#000099 1px solid;
background:#000099;
padding:4px 6px 4px 6px;
margin:2px;
color:#fff;
font-weight:bold;
}
div.page span.disable{
border:1px solid ;
padding:2px 5px 2px 5px;
margin:2px;
color:#ddd;
}
div.page form{
display:inline;
}
PHP+MySQL分页原理实现
41204 学习 · 185 问题
相似问题