Home >php教程 >php手册 >PHP实现数据分页显示,php实现分页显示

PHP实现数据分页显示,php实现分页显示

WBOY
WBOYOriginal
2016-06-13 08:39:081130browse

PHP实现数据分页显示,php实现分页显示

  分页在后台管理中是经常使用的功能,分页显示方便大量数据的管理。

  实例代码如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
</head>
<body>
<?<span>php 
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
   
    </span><span>mysql_query</span>("set names utf8"<span>);
    </span><span>mysql_select_db</span>("zhiye",<span>$con</span><span>);
    
    </span><span>$pageSize</span> = 1;      <span>//</span><span>每页显示的数量</span>
    <span>$rowCount</span> = 0;      <span>//</span><span>要从数据库中获取</span>
    <span>$pageNow</span> = 1;        <span>//</span><span>当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span>if</span> (!<span>empty</span>(<span>$_GET</span>['pageNow'<span>])){
        </span><span>$pageNow</span> = <span>$_GET</span>['pageNow'<span>];
    }
    
    </span><span>$pageCount</span> = 0;   <span>//</span><span>表示共有多少页</span>
    
    <span>$sql1</span> = "select count(id) from user"<span>;
    </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>);
    
    </span><span>if</span>(<span>$row1</span>=<span>mysql_fetch_row</span>(<span>$res1</span><span>)){
        </span><span>$rowCount</span> = <span>$row1</span>[0<span>];
    }
    
    </span><span>//</span><span>计算共有多少页,ceil取进1</span>
    <span>$pageCount</span> = <span>ceil</span>((<span>$rowCount</span>/<span>$pageSize</span><span>));
    
    </span><span>//</span><span>使用sql语句时,注意有些变量应取出赋值。</span>
    <span>$pre</span> = (<span>$pageNow</span>-1)*<span>$pageSize</span><span>;
    
    </span><span>$sql2</span> = "select * from user limit <span>$pre</span>,<span>$pageSize</span>"<span>;
    </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);</span>
  
   <span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$res2</span><span>)){
        </span><span>echo</span> <span>$row</span>['user_name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['email']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['password']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['tel']."<br>"<span>;
   }
    </span><span>for</span> (<span>$i</span>=1;<span>$i</span><=<span>$pageCount</span>;<span>$i</span>++<span>){
        </span><span>echo</span> "<a href='userList.php?pageNow=<span>$i</span>'><span>$i</span></a> "<span>;
    }
</span>?>
</body>
</html>

   当有大量数据时,就不能使用上述方法。

<?<span>php 
    </span><span>$con</span> = <span>mysql_connect</span>("localhost","root",""<span>);
   
    </span><span>mysql_query</span>("set names utf8"<span>);
    </span><span>mysql_select_db</span>("zhiye",<span>$con</span><span>);
    
    </span><span>$pageSize</span> = 1;      <span>//</span><span>每页显示的数量</span>
    <span>$rowCount</span> = 0;      <span>//</span><span>要从数据库中获取</span>
    <span>$pageNow</span> = 1;        <span>//</span><span>当前显示第几页
    
    //如果有pageNow就使用,没有就默认第一页。</span>
    <span>if</span> (!<span>empty</span>(<span>$_GET</span>['pageNow'<span>])){
        </span><span>$pageNow</span> = <span>$_GET</span>['pageNow'<span>];
    }
    
    </span><span>$pageCount</span> = 0;   <span>//</span><span>表示共有多少页</span>
    
    <span>$sql1</span> = "select count(id) from user"<span>;
    </span><span>$res1</span> = <span>mysql_query</span>(<span>$sql1</span><span>);
    
    </span><span>if</span>(<span>$row1</span>=<span>mysql_fetch_row</span>(<span>$res1</span><span>)){
        </span><span>$rowCount</span> = <span>$row1</span>[0<span>];
    }
    
    </span><span>//</span><span>计算共有多少页,ceil取进1</span>
    <span>$pageCount</span> = <span>ceil</span>((<span>$rowCount</span>/<span>$pageSize</span><span>));
    
    </span><span>//</span><span>使用sql语句时,注意有些变量应取出赋值。</span>
    <span>$pre</span> = (<span>$pageNow</span>-1)*<span>$pageSize</span><span>;
    
    </span><span>$sql2</span> = "select * from user limit <span>$pre</span>,<span>$pageSize</span>"<span>;
    </span><span>$res2</span> = <span>mysql_query</span>(<span>$sql2</span><span>);
    
    </span><span>//</span><span>$sql = "select * from user";
    //$res = mysql_query($sql,$con);</span>
  
   <span>while</span>(<span>$row</span>=<span>mysql_fetch_assoc</span>(<span>$res2</span><span>)){
        </span><span>echo</span> <span>$row</span>['user_name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['name']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['email']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['password']."<br>"<span>;
        </span><span>echo</span> <span>$row</span>['tel']."<br>"<span>;
   }
   </span><strong><span>if</span>(<span>$pageNow</span>>1<span>){
       </span><span>$prePage</span> = <span>$pageNow</span>-1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$prePage</span>'>pre</a> "<span>;
   }
   </span><span>if</span>(<span>$pageNow</span><<span>$pageCount</span><span>){
       </span><span>$nextPage</span> = <span>$pageNow</span>+1<span>;
       </span><span>echo</span> "<a href='userList.php?pageNow=<span>$nextPage</span>'>next</a> "<span>;
       </span><span>echo</span> "当前页{<span>$pageNow</span>}/共{<span>$pageCount</span>}页"<span>;
   }
   </span><span>echo</span> "<br/><br/>"<span>;
   </span>?>
 
   <form action="userList.php">
        <input type="text" name="pageNow">
        <input type="submit" value="GO">
   </form></strong>

 

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