php分页效果

PHP+MYSQL分页原理

1、  sql语句中的limit用法

 select * from table limit 开始位置(从0开始),操作数

 select * from table limit 0,20  --取前20个数据

 select * from table limit 10,20  ---取11条到30条数据

2、  学习分页的一种公式

(1)       分页原理

  所谓的分页显示,也就是将数据库中的结果集,以一定的条数显示出来。

(2)       如何分段(以10条数据为单位)

  前10条记录:

select * from table limit 0,10

  第11条到20条:

 select * from table limit 10,10

  第21条到30条: 

select * from table limit 20,10

(3)       得到公式

  (当前页数-1)*每页条数,每页条数

 select * from table limit($Page-1)*$PageSize,$PageSize;

3、  实例代码

 

<?php

  //分页效果2

  include("conn.php");

  $server_url=$_SERVER["REQUEST_URI"]; //从浏览器得到URL

  $parse_url=parse_url($server_url);   //将浏览器得到的URL转换为数组

  $url=$parse_url["path"];             //得到url地址字符串

  $pagesize=5;                         //设置分页数目

  $sql1="select * from article";

  $query1=mysql_query($sql1);

  $num=mysql_num_rows($query1);        //得到查询的表的所有数据条数

  $size=(int)($num/$pagesize)+1;      //得到总页数

  $pageindex=0;

  if(!empty($_GET["page"]))

  {

    $pageindex=$_GET["page"];     //取得页面索引

  }

 

  $pageindex*=5;

  $sql2=select($pageindex);          //得到分页查询的sql语句

  echo $sql2;

  $query2=mysql_query($sql2);

  while ($data=mysql_fetch_array($query2))

  {

    echo "<h2>标题:".$data["title"]."</h2>&nbsp;&nbsp;".$data["date"]."<br /><b>".$data["contents"]."</b><hr />";

  }

?>

 

<form action="list.php" method="get"">

  <?php

    for($index=0;$index<$size;$index++)

    {

    echo "<a href=$url?page=".$index.">第".($index+1)."页</a>";

    }

  ?>

</form>

<?php

 function select($index)

 {

    $sqlselect="select * from article limit $index,5";

    return $sqlselect;

 }

?> 
原文地址:https://www.cnblogs.com/luodao1991/p/3121202.html