在贴一下代码吧
<?php $localh">容易的利用php谢了一个分页的功能模块
简单的利用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> "Copy after login"; echo "
"; //上一页 、下一页 $pageprev = $page -1 ; if($page > $total){ $pagenext = $total; } else{ $pagenext = $page +1; } // 做链接 跳转; echo ""; while($row = mysql_fetch_assoc($result)){ $id = $row['id']; $name = $row['name']; echo " ID NAME "; } echo " ".$id." ".$name." 上一页 丨下一页
"; mysql_free_result($result); mysql_close($conn);?>关键点: 1、$sql = "select * from page order by id limit {$offset},{$pagesize}";
这两点是关键能体现 分页技术 和php 代码艺术的地方~
和大家一起学习吧~
Statement of this WebsiteThe 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.cnLatest Articles by Author
2024-10-22 09:46:29 2024-10-13 13:53:41 2024-10-12 12:15:51 2024-10-11 22:47:31 2024-10-11 19:36:51 2024-10-11 15:50:41 2024-10-11 15:07:41 2024-10-11 14:21:21 2024-10-11 12:59:11 2024-10-11 12:17:31Latest Issues<caption> <colgroup><col> <thead><tbody><tfoot> How to use these tagsFrom 1970-01-01 08:00:00000Popular RecommendationsPopular TutorialsMore>
JAVA Beginner's Video Tutorial2596753 Latest DownloadsMore>