(转载)用PHP实现翻页

(转载)http://blog.csdn.net/emili/article/details/5221744

原文参考http://www.cnblogs.com/xxcainiao/archive/2009/04/18/1438482.html

稍作修改,做了一个goto按钮:

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>PHP分页</title>
</head>

<body>

<?php
$mysql_server_name = "localhost";
$mysql_username = "a";
$mysql_password = "a";
$mysql_database = "a";
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("couldn't open SQL Server $servername <br>");


$page=$_GET["page"];
if( isset($_POST["page"]) && $_POST["page"]!="")
{
    $page=$_POST['page'];
}
if($page=="")
{
    $page=1;
}

if(is_numeric($page))
{
    $page_size=12;  //每页多少条数据
    $query="select count(*) as total from rpt_safeboxins_daily";
    $result = mysql_db_query($mysql_database,$query,$conn);
    $row= mysql_fetch_row($result);
    $message_count=$row[0];

    $page_count=ceil($message_count/$page_size);
    $offset=($page-1)*$page_size;
   
    $result=mysql_db_query($mysql_database,"select * from rpt_safeboxins_daily limit $offset,$page_size",$conn);
    while($row = mysql_fetch_row($result))
    {
        echo "$row[0],$row[1],$row[2]<br>";
    }

}

?>
==========================================================================================================<br>
当前页码:<?php echo $page;?>/<?php echo $page_count;?>
记录条数:<?php echo $message_count;?>

<form   method="POST" action="<?php echo $PHP_SELF ?>" target="_self">
<?php
    if($page!=1)
    {
        echo "<a href=a.php?page=1>首页</a> | ";
        echo "<a href=a.php?page=".($page-1).">上一页</a>   ";
    }
?>         
    <input type=submit value="goto">
    <input type="text" size=4 name="page">
<?php
    if($page<$page_count)
    {
        echo "<a href=a.php?page=".($page+1).">下一页</a> | ";
        echo "<a href=a.php?page=".$page_count.">尾页</a>";
    }
    mysql_free_result($sql);
    mysql_close($conn);
?>
                   
</form>
</body>
</html>

原文地址:https://www.cnblogs.com/Robotke1/p/3190536.html