首页 >后端开发 >php教程 >简单的利用php谢了一个分页的功能模块,php分页功能模块_PHP教程

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

WBOY
WBOY原创
2016-07-13 09:51:08848浏览

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

先贴张图看看效果

 

 

 

在贴一下代码吧

<?<span>php
    
    </span><span>$localhost</span> = "localhost"<span>;
    </span><span>$username</span> = "root"<span>;
    </span><span>$password</span> = "root"<span>;
    </span><span>$db</span> = "test";         <span>//</span><span>信息</span>
    
    <span>$pagesize</span> = 5<span>;
                              
    </span><span>$conn</span> = <span>mysql_connect</span>(<span>$localhost</span>,<span>$username</span>,<span>$password</span>); <span>//</span><span>链接数据库</span>
    
        <span>if</span>(!<span>$conn</span><span>){
            </span><span>echo</span> "数据库链接失败".<span>mysql_error</span><span>();
        }
    
    </span><span>mysql_query</span>("SET NAMES 'UTF8'");    <span>//</span><span>编码转化</span>

    <span>$db_select</span> = <span>mysql_select_db</span>(<span>$db</span>);  <span>//</span><span>选择表

                                        //查询记录总数</span>
    <span>$total_sql</span> = "select COUNT(*) from page"<span>;
    </span><span>$total_result</span> = <span>mysql_query</span>(<span>$total_sql</span><span>);
    </span><span>$total_row_arr</span> = <span>mysql_fetch_row</span>(<span>$total_result</span><span>);
    </span><span>$total_row</span> = <span>$total_row_arr</span>[0];         <span>//</span><span>总条数

    //总页数</span>
    <span>$total</span> = <span>ceil</span>(<span>$total_row</span> / <span>$pagesize</span><span>);
    
    </span><span>//</span><span>当前页数</span>
    <span>$page</span> = @<span>$_GET</span>['p'] ? <span>$_GET</span>['p'] : 1<span>;
    
     </span><span>//</span><span>limit 下限</span>
    <span>$offset</span> = (<span>$page</span> - 1)*<span>$pagesize</span><span>;
    
    
    </span><span>$sql</span> = "select * from page order by id limit {<span>$offset</span>},{<span>$pagesize</span>}"<span>;
    
    </span><span>$result</span> = <span>mysql_query</span>(<span>$sql</span><span>);
    
    </span><span>echo</span> "<p>PHP分页代码的小模块</p>"<span>;
    </span><span>echo</span> "<table border=1 cellspacing=0 WIDTH=60% align=center>"<span>;
    </span><span>echo</span> "<tr><td>ID</td><td>NAME</td></tr>"<span>;
    
    </span><span>while</span>(<span>$row</span> = <span>mysql_fetch_assoc</span>(<span>$result</span><span>)){
        </span><span>$id</span> = <span>$row</span>['id'<span>];
        </span><span>$name</span> = <span>$row</span>['name'<span>];
        </span><span>echo</span> "<tr><td>".<span>$id</span>."</td><td>".<span>$name</span>."</td></tr>"<span>;
    }
    
    </span><span>echo</span> "</table>"<span>;
    </span><span>//</span><span>上一页 、下一页</span>
    
    <span>$pageprev</span> = <span>$page</span> -1<span> ;
        </span><span>if</span>(<span>$page</span> > <span>$total</span><span>){
            </span><span>$pagenext</span> = <span>$total</span><span>;
        } </span><span>else</span><span>{
            </span><span>$pagenext</span> = <span>$page</span> +1<span>;
        }
    
    </span><span>//</span><span> 做链接 跳转;</span>
    
    <span>echo</span> "<h3> <a href='page.php?p={<span>$pageprev</span>}'>上一页</a> 丨<a href='page.php?p={<span>$pagenext</span>}'>下一页</a></h3>"<span>;
    
    
    
    </span><span>mysql_free_result</span>(<span>$result</span><span>);
    </span><span>mysql_close</span>(<span>$conn</span><span>);
</span>?>

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

     2、echo "

上一页下一页

";

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

和大家一起学习吧~

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1015277.htmlTechArticle简单的利用php谢了一个分页的功能模块,php分页功能模块 先贴张图看看效果 在贴一下代码吧 ? php $localhost = "localhost" ; $username = "root" ; $pa...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn