Home  >  Article  >  php教程  >  php分页代码

php分页代码

WBOY
WBOYOriginal
2016-06-14 00:02:371275browse

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

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

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


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


<h2>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