>  기사  >  php教程  >  php分页代码

php分页代码

WBOY
WBOY원래의
2016-06-14 00:02:371277검색

今天在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 "
"; ?>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.