search
Homephp教程php手册PHP 分页代码与分页原理解析

本文章来给各位php入门者提供在php与mysql实现分页代码与原理,在mysql中支持limit 0,1这样就是查询0,1条记录了,有了它在php中分页就简单快速多了。

所谓分页显示,也就是将数据库中的结果集人为的分成一段一段的来显示,这里需要两个初始的参数:
每页多少条记录($PageSize)?
当前是第几页($CurrentPageID)?
现在只要再给我一个结果集,我就可以显示某段特定的结果出来。
至于其他的参数,比如:上一页($PreviousPageID)、下一页($NextPageID)、总页数($numPages)等等,都可以根据前边这几个东西得到。
以MySQL数据库为例,如果要从表内截取某段内容,sql语句可以用:select * from table limit offset, rows。看看下面一组sql语句,尝试一下发现其中的规率。

 代码如下 复制代码
select * from table limit 0,10 // 前10条记录
select * from table limit 10,10 // 第11至20条记录
select * from table limit 20,10 // 第21至30条记录

……

这一组sql语句其实就是当$PageSize=10的时候取表内每一页数据的sql语句,我们可以总结出这样一个模板:

 代码如下 复制代码

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

拿这个模板代入对应的值和上边那一组sql语句对照一下看看是不是那么回事。搞定了最重要的如何获取数据的问题以后,剩下的就仅仅是传递参数,构造合适的sql语句然后使用php从数据库内获取数据并显示了。以下我将用具体代码加以说明。

 代码如下 复制代码



<br>ShowData<br>


ShowData


    //连接数据库   
    $page = 1;
        $db = mysql_connect('127.0.0.1','root','toor');
        mysql_select_db('test',$db);
   
        $pagesize = 3;    //每页显示到数量
   
    //计算一共有多少记录,用于计算页数
    $rs = mysql_query("select count(*) from info",$db);
    $row = @mysql_fetch_array($rs);
    $numrows = $row[0];

    //计算页数
    $pages = intval($numrows / $pagesize);
    if ($numrows % $pagesize)
    {
        $pages++;
    }

    //设置页数
    if (isset($_GET['page']))
    {   
        $page = intval($_GET['page']);
    }
    else
    {
        $page = 1;        //其他情况,都指向第一页
    }
   
    //计算记录的偏移量
    $offset = $pagesize * ($page - 1);

    //读取指定记录
    $rs = mysql_query("select * from info order by id limit $offset,$pagesize",$db);

    //把数据用表格显示出来
    if ($row = @mysql_fetch_array($rs))
    {
        $i = 0;
        ?>
       


       
       
       
       
                    do{
                $i++;
        ?>
       
       
       
       
                    }
            //循环显示数据
            while ($row = mysql_fetch_array($rs));
            echo "

       

ID


       

DOC

=$row['id']?> =$row['doc']?>
";
    }
    echo "
共".$pages."页(".$page."/".$pages.")";
    for ($i = 1;$i     {
        echo "[".$i."]";
    }
    echo "[".$page."]";
    for ($i = $page + 1;$i     {
        echo "[".$i."]";
    }
    echo "
";

    ?>


总结,分页就根据用户点击连接,然后由于php获取page值再经过一系列算法得出当前多少页面,然后取多少要就显示出你要查看的记录了。



本文地址:

转载随意,但请附上文章地址:-)

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

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor