search
Homephp教程php手册php分页代码

php分页代码

Jun 14, 2016 am 12:02 AM
phpcodePaginationOpen sourceprogrammingprogramming languagesoftware development

今天在BT5中一不小心发现了自己实习时,看PHP写的一个php分页代码。没啥看点,看了跟网上他们写的也没啥不同。

贴出来也给跟我一样的菜鸟瞅瞅吧。我这php算是速成产品,当时写个这分页,也出现不少问题。不过问题出现的多,才能学到东西,哈哈·····喜欢问题···

我的数据库就两个字段一个id,一个doc,id还是自增字段。


<title><span style="color: #000000;">
ShowData
</span></title>


<h2 id="ShowData">ShowData</h2>
<span style="color: #000000;">php
    </span><span style="color: #008000;">//</span><span style="color: #008000;">连接数据库    </span>
    <span style="color: #800080;">$page</span> = 1<span style="color: #000000;">;
        </span><span style="color: #800080;">$db</span> = <span style="color: #008080;">mysql_connect</span>('127.0.0.1','root','toor'<span style="color: #000000;">);
        </span><span style="color: #008080;">mysql_select_db</span>('test',<span style="color: #800080;">$db</span><span style="color: #000000;">);
    
        </span><span style="color: #800080;">$pagesize</span> = 3;    <span style="color: #008000;">//</span><span style="color: #008000;">每页显示到数量
    
    //计算一共有多少记录,用于计算页数</span>
    <span style="color: #800080;">$rs</span> = <span style="color: #008080;">mysql_query</span>("select count(*) from info",<span style="color: #800080;">$db</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$row</span> = @<span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$rs</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$numrows</span> = <span style="color: #800080;">$row</span>[0<span style="color: #000000;">];

    </span><span style="color: #008000;">//</span><span style="color: #008000;">计算页数</span>
    <span style="color: #800080;">$pages</span> = <span style="color: #008080;">intval</span>(<span style="color: #800080;">$numrows</span> / <span style="color: #800080;">$pagesize</span><span style="color: #000000;">);
    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$numrows</span> % <span style="color: #800080;">$pagesize</span><span style="color: #000000;">)
    {
        </span><span style="color: #800080;">$pages</span>++<span style="color: #000000;">;
    }

    </span><span style="color: #008000;">//</span><span style="color: #008000;">设置页数</span>
    <span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">]))
    {    
        </span><span style="color: #800080;">$page</span> = <span style="color: #008080;">intval</span>(<span style="color: #800080;">$_GET</span>['page'<span style="color: #000000;">]);
    }
    </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
    {
        </span><span style="color: #800080;">$page</span> = 1;        <span style="color: #008000;">//</span><span style="color: #008000;">其他情况,都指向第一页</span>
<span style="color: #000000;">    }
    
    </span><span style="color: #008000;">//</span><span style="color: #008000;">计算记录的偏移量</span>
    <span style="color: #800080;">$offset</span> = <span style="color: #800080;">$pagesize</span> * (<span style="color: #800080;">$page</span> - 1<span style="color: #000000;">);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">读取指定记录</span>
    <span style="color: #800080;">$rs</span> = <span style="color: #008080;">mysql_query</span>("select * from info order by id limit <span style="color: #800080;">$offset</span>,<span style="color: #800080;">$pagesize</span>",<span style="color: #800080;">$db</span><span style="color: #000000;">);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">把数据用表格显示出来</span>
    <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$row</span> = @<span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$rs</span><span style="color: #000000;">))
    {
        </span><span style="color: #800080;">$i</span> = 0<span style="color: #000000;">;
        </span>?>
        
php do{ $i++; ?> php } //循环显示数据while ($row = mysql_fetch_array($rs)); echo "

ID

DOC

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

 

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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