在贴一下代码吧

<?php        $localh">




















首页 >后端开发 >php教程 >容易的利用php谢了一个分页的功能模块

容易的利用php谢了一个分页的功能模块

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-13 12:21:48879浏览

简单的利用php谢了一个分页的功能模块

先贴张图看看效果

 

 

 

在贴一下代码吧

<span style="color: #000000;">php        </span><span style="color: #800080;">$localhost</span> = "localhost"<span style="color: #000000;">;    </span><span style="color: #800080;">$username</span> = "root"<span style="color: #000000;">;    </span><span style="color: #800080;">$password</span> = "root"<span style="color: #000000;">;    </span><span style="color: #800080;">$db</span> = "test";         <span style="color: #008000;">//</span><span style="color: #008000;">信息</span>        <span style="color: #800080;">$pagesize</span> = 5<span style="color: #000000;">;                                  </span><span style="color: #800080;">$conn</span> = <span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$localhost</span>,<span style="color: #800080;">$username</span>,<span style="color: #800080;">$password</span>); <span style="color: #008000;">//</span><span style="color: #008000;">链接数据库</span>            <span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$conn</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">echo</span> "数据库链接失败".<span style="color: #008080;">mysql_error</span><span style="color: #000000;">();        }        </span><span style="color: #008080;">mysql_query</span>("SET NAMES 'UTF8'");    <span style="color: #008000;">//</span><span style="color: #008000;">编码转化</span>    <span style="color: #800080;">$db_select</span> = <span style="color: #008080;">mysql_select_db</span>(<span style="color: #800080;">$db</span>);  <span style="color: #008000;">//</span><span style="color: #008000;">选择表                                        //查询记录总数</span>    <span style="color: #800080;">$total_sql</span> = "select COUNT(*) from page"<span style="color: #000000;">;    </span><span style="color: #800080;">$total_result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$total_sql</span><span style="color: #000000;">);    </span><span style="color: #800080;">$total_row_arr</span> = <span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$total_result</span><span style="color: #000000;">);    </span><span style="color: #800080;">$total_row</span> = <span style="color: #800080;">$total_row_arr</span>[0];         <span style="color: #008000;">//</span><span style="color: #008000;">总条数    //总页数</span>    <span style="color: #800080;">$total</span> = <span style="color: #008080;">ceil</span>(<span style="color: #800080;">$total_row</span> / <span style="color: #800080;">$pagesize</span><span style="color: #000000;">);        </span><span style="color: #008000;">//</span><span style="color: #008000;">当前页数</span>    <span style="color: #800080;">$page</span> = @<span style="color: #800080;">$_GET</span>['p'] ? <span style="color: #800080;">$_GET</span>['p'] : 1<span style="color: #000000;">;         </span><span style="color: #008000;">//</span><span style="color: #008000;">limit 下限</span>    <span style="color: #800080;">$offset</span> = (<span style="color: #800080;">$page</span> - 1)*<span style="color: #800080;">$pagesize</span><span style="color: #000000;">;            </span><span style="color: #800080;">$sql</span> = "select * from page order by id limit {<span style="color: #800080;">$offset</span>},{<span style="color: #800080;">$pagesize</span>}"<span style="color: #000000;">;        </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">echo</span> "<p>PHP分页代码的小模块</p>"<span style="color: #000000;">;    </span><span style="color: #0000ff;">echo</span> "
"; echo ""; while($row = mysql_fetch_assoc($result)){ $id = $row['id']; $name = $row['name']; echo ""; } echo "
ID NAME
".$id." ".$name."
"; //上一页 、下一页 $pageprev = $page -1 ; if($page > $total){ $pagenext = $total; } else{ $pagenext = $page +1; } // 做链接 跳转; echo "

上一页下一页

"; mysql_free_result($result); mysql_close($conn);?>

关键点: 1、$sql = "select * from page order by id limit {$offset},{$pagesize}";

     2、echo "

上一页下一页

";

这两点是关键能体现 分页技术 和php 代码艺术的地方~

和大家一起学习吧~

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn