Home  >  Article  >  php教程  >  简单的php分页代码

简单的php分页代码

WBOY
WBOYOriginal
2016-06-21 08:52:581155browse
<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>



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