php简单分页

php简单分页

<!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=GBK" />
<title>分页查询</title>
</head>


<body>

	<table width="550" border="1" cellpadding="0" cellspacing="1"
		bordercolor="#FFFFFF" bgcolor="#999999">
		<tr align="center" bgcolor="#f0f0f0">
			<td width="221">公告标题</td>
			<td width="329">公告内容</td>
		</tr>

<?php 
error_reporting(E_ALL & ~E_NOTICE);
    $conn=mysql_connect("localhost","root","root") or die ("数据库服务器连接错误".mysql_errno());
    mysql_select_db("phpmysql",$conn) or die("数据库访问错误".mysql_errno());
    mysql_query("set names gbk");
    //分页
    //获取超链接传过来的值
     $page=$_GET[page];
    if($page==""){
       $page=1;
    }
    if (is_numeric($page)){//判断变量$page是否为数字,如果是则返回true
        echo   "这里的值".$page;
        $page_size=2;//每页显示4条记录
        $query="select count(*) as total from tb_affiche order by id desc";//查询符合条件的记录总数
        $result=mysql_query($query);
        $message_count=mysql_result($result, 0,"total");//要显示的记录总数
        /**  根据记录总数除以每页显示的记录数求出所分的页数 **/
        $page_count=ceil($message_count/$page_size);
        $offset=($page-1)*$page_size;//计算下一页从第几条数据开始
        $sql=mysql_query("select * from tb_affiche order by id desc limit $offset,$page_size");
        $row=mysql_fetch_object($sql);//获取查询信息
        if(!$row){
            echo "<font color='red'>暂无公告信息!</font>";
        }
 
        do{
 ?>
             <tr bgcolor="#FFFFFF">
                <td><?php echo $row->title;?></td>
                 <td><?php echo $row->content;?></td>
             </tr>
 <?php
               }while($row=mysql_fetch_object($sql));
             
            


}?>
</table>
 <table width="550" border="0" cellspacing="0" cellpadding="0">
    <tr>
       <!-- 翻页条 -->
       <td width="37%">  页次<?php echo $page;?>/<?php echo $page_count?>页 记录:<?php echo $message_count;?>条  </td>
       <td width="63%" align="right"></td>
       <?php 
         /* 如果不是当前首页 */
       if($page!=1){
           /* 显示首页超链接 */
           echo "<a href=page_affiche.php?page=1>首页</a> ";
           /* 显示上一页的超级链接*/
           echo "<a href=page_affiche.php?page=".($page-1).">上一页</a> ";
       }
       /* 当前页不是尾页
        */
       if($page<$page_count){
           /* 显示下一页的超级链接 */
           echo "<a href=page_affiche.php?page=".($page+1).">下一页</a> ";
           echo "<a href=page_affiche.php?page=".$page_count.">尾页</a>";
       }
    mysql_free_result($sql);
    mysql_close($conn);
       ?>
    </tr>
 </table>
</body>
</html>

效果图

发布了70 篇原创文章 · 获赞 1 · 访问量 488

猜你喜欢

转载自blog.csdn.net/zhupengqq1/article/details/103953961