当前位置:博客首页>>PHP >> 阅读正文

自学php分页原理小结..

作者: 郑晓 分类: PHP 发布于: 2011-04-27 16:29 浏览(2,920) 没有评论


//定每页显示多少条
//count(*)查询总条数

//计算总页数ceil()

//判断$_GET 并赋值当前页数

//mysql查询 limit (当前页数-1)*每页条数,每页条数

//如果总条数不为空,进行循环mysql_fetch_array()

//$_SERVER[‘PHP_SELF’]
//上一页?page=当前页-1
//下一页?page=当前页+1

<?php

include("conn.php");



$perpagenum = 3;

$total = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM message"));

$Total = $total[0];

$Totalpage =ceil($Total/$perpagenum);



if(!isset($_GET['page'])||!intval($_GET['page'])||$_GET['page']>$Totalpage){

    $page = 1;

}else{

    $page = $_GET['page'];

}

$start = ($page-1)*$perpagenum;

$sql = "SELECT * FROM message ORDER BY id DESC LIMIT $start,$perpagenum";

$result = mysql_query($sql);

$contents = mysql_fetch_array($result);

?>

<table width="849" height="90">

<?php

if($total){

    do{

        $user = $contents['user'];

        $contact = $contents['contact'];

        $content = $contents['content'];

        $date    = $contents['date'];

    ?>

      <tr bgcolor="#B3DBB5">

    <td width="193" height="22">昵称:<?php echo $user;?></td>

    <td width="373"><?php echo $contact;?></td>

    <td width="261">发表于:<?php echo $date;?></td>

  </tr>

  <tr bgcolor="#D8D8D8">

    <td height="auto" colspan="3"><?php echo $content;?></td>

  </tr>

    <?php

    }

     while($contents=mysql_fetch_array($result));



     $per = $page-1;

     $next= $page+1;

     echo "<center>共有".$Total."条留言,每页".$perpagenum."条,共".$page."/".$Totalpage."页。";

     if($page!=1){

        echo "<a href=".$_SERVER['PHP_SELF'].">首页</a>".

        "<a href=".$_SERVER['PHP_SELF']."?page=$per".">上一页</a>";

     }

     if($page!=$Totalpage){

        echo "<a href=".$_SERVER['PHP_SELF']."?page=$next".">下一页</a>";

        echo "<a href=".$_SERVER['PHP_SELF']."?page=$Totalpage".">尾页</a>";

     }



    ?>

<?php

}

?>

</table>
↓↓微信扫码请我吃份正宗的烤面筋,可带劲啦↓↓
       

本文采用知识共享署名-非商业性使用 3.0 中国大陆许可协议进行许可,转载时请注明出处及相应链接。

本文永久链接: https://www.zh30.com/principle-of-self-page-summary-of-php.html