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

PHP实现数据分页显示

May 26, 2016 am 08:19 AM

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

  实例代码如下:



    <meta charset="utf-8">
    <title>用户列表</title>


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

Copy after login

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)