Home  >  Article  >  php教程  >  PHP_MySQL数据分页

PHP_MySQL数据分页

PHP中文网
PHP中文网Original
2016-05-25 17:15:131262browse

PHP_MySQL数据分页

<?php
if($_GET["page"]){
    $page=$_GET["page"];#获取要显示的页码
}else{
    $page=1;#没有参数传入时,显示第一页
}
$col=13;#每页显示记录条数
$p=($page-1)*$col;
mysql_connect("127.0.0.1:3306","root","");
mysql_query("set names gbk");
mysql_select_db("php");#数据库中需要事先建product表并插入数据,含有name和price字段。
$result=mysql_query("select * from product limit ".$p.",".$col."");
echo "
<table BORDER=1>
<tr>
<th>name</th>
<th>price</th>
</tr>
";
while($rs=mysql_fetch_object($result))
{
echo "<tr>
<td>".$rs->name."</td>
<td>".$rs->price."</td>
</tr>
";
}
echo "</table>";
$count=mysql_query("select name from product");#取得记录总条数
$num = mysql_num_rows($count)/$col;#计算页数
$num=ceil($num);#无条件小数进位确定总页数
mysql_close();
if($page==1){
    echo &#39;上一页&#39;;
}else{
    echo "<a href=?page=".($page-1).">上一页</a>";
}
echo " 第".$page."页 ";
if($page==$num){
    echo &#39;下一页<br>&#39;;
}else{
    echo "<a href=&#39;?page=".($page+1)."&#39;>下一页</a><br>";
}
 
if($page==1){
}else{
    echo "<a href=&#39;?page=1&#39;>第一页</a><<<";
}
 
for($i=-2;$i<=2;$i++){
    if(($page+$i)>$num||($page+$i)<1){
 
    }else{
        echo " <a href=?page=".($page+$i).">".($page+$i)."</a> ";
    }
}
if($page==$num){
}else{
    echo ">>><a href=&#39;?page=".$num."&#39;>最后一页</a><br>";
}
?>
<form action="" method=&#39;get&#39;>
跳转到<input type=&#39;text&#39; name=&#39;page&#39;>页
<input type=&#39;submit&#39; value="确定">
</form>

 以上就是PHP_MySQL数据分页的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn