首页 > php教程 > php手册 > 正文

简单的php分页代码

WBOY
发布: 2016-06-21 08:52:58
原创
1159 人浏览过
<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span> <span class="vars">$conn</span><span> = mysql_connect(</span><span class="string">'localhost'</span><span>,</span><span class="string">'root'</span><span>,</span><span class="string">'mysql'</span><span>); </span></span></li>
<li class="alt"><span> <span class="vars">$db</span><span> = mysql_select_db(</span><span class="string">"wiki"</span><span>); </span></span></li>
<li><span> <span class="vars">$q</span><span> = mysql_query(</span><span class="string">"select * from wiki_doc"</span><span>); </span></span></li>
<li class="alt"><span>  </span></li>
<li><span> <span class="vars">$num</span><span> = mysql_num_rows(</span><span class="vars">$q</span><span>); </span></span></li>
<li class="alt"><span> <span class="vars">$perpage</span><span> = 5; </span></span></li>
<li><span> <span class="vars">$totalpage</span><span> = @</span><span class="func">ceil</span><span>(</span><span class="vars">$num</span><span>/</span><span class="vars">$perpage</span><span>); </span></span></li>
<li class="alt"><span> <span class="vars">$page</span><span> =</span><span class="vars">$_GET</span><span>[</span><span class="string">'page'</span><span>]; </span></span></li>
<li><span>  </span></li>
<li class="alt"><span> <span class="keyword">if</span><span>(!isset(</span><span class="vars">$page</span><span>)){ </span></span></li>
<li><span>   <span class="vars">$page</span><span> = 1; </span></span></li>
<li class="alt"><span>    </span></li>
<li><span> } </span></li>
<li class="alt"><span>  </span></li>
<li><span> <span class="comment">//$start = ($page-1)*$perpage;</span><span> </span></span></li>
<li class="alt"><span> <span class="vars">$start</span><span> = (</span><span class="vars">$page</span><span>-1)*</span><span class="vars">$perpage</span><span>; </span></span></li>
<li><span> <span class="vars">$qq</span><span>=mysql_query(</span><span class="string">"select * from wiki_doc limit $start,$perpage"</span><span>); </span></span></li>
<li class="alt"><span> <span class="vars">$info</span><span> = </span><span class="keyword">array</span><span>(); </span></span></li>
<li><span> <span class="keyword">while</span><span>(</span><span class="vars">$result</span><span>=mysql_fetch_array(</span><span class="vars">$qq</span><span>)){ </span></span></li>
<li class="alt"><span>    <span class="vars">$info</span><span>[] = </span><span class="vars">$result</span><span>; </span></span></li>
<li><span> } </span></li>
<li class="alt"><span>  </span></li>
<li><span> <span class="func">echo</span><span> </span><span class="string">"<table>"<span>; </span><li class="alt"><span> <span class="keyword">foreach</span><span>(</span><span class="vars">$info</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$key</span><span>){ </span></span></li>
<li><span>   <span class="func">echo</span><span> </span><span class="string">"<tr>
<td>$key[did]</td>
<td> </td>
<td>$key[title]</td>
</tr>"</span><span>; </span></span></li>
<li class="alt"><span> } </span></li>
<li><span> <span class="func">echo</span><span> </span><span class="string">"</span></span></li>
</table>"</span><span>; </span></span></li>
<li class="alt"><span>  <span class="keyword">if</span><span>(</span><span class="vars">$page</span><span>!=1){?> </span></span></li>
<li>
<span>    <a class="string">"page2.php?page=<?php  echo $page-1; ?>"</a></span><span>>Prev </span>
</li>
<li class="alt"><span>    <?php  </span></span></li>
<li><span>  } </span></li>
<li class="alt"><span>  <span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1;</span><span class="vars">$i</span><span><span class="vars">$totalpage</span><span>;</span><span class="vars">$i</span><span>++){ </span></span></span></li>
<li><span>    ?> </span></li>
<li class="alt">
<span>    <a class="string">"page2.php?page=<?php  echo $i; ?>"</a></span><span>><?php  </span><span class="func">echo</span><span> </span><span class="vars">$i</span><span>; ?> </span></span>
</li>
<li><span>    <?php  </span></span></li>
<li class="alt"><span>     </span></li>
<li><span>  } </span></li>
<li class="alt"><span>  <span class="keyword">if</span><span>(</span><span class="vars">$page</span><span><span class="vars">$totalpage</span><span>){?> </span></span></span></li>
<li>
<span>  <a class="string">"page2.php?page=<?php  echo $page+1; ?>"</a></span><span>>Next </span>
</li>
<li class="alt"><span>   <?php   </span></span></li>
<li><span>  }?>  </span></li>
</ol>
登录后复制



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板