Heim >php教程 >php手册 >简单的利用php谢了一个分页的功能模块,php分页功能模块

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

WBOY
WBOYOriginal
2016-06-13 09:01:56974Durchsuche

简单的利用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 代码艺术的地方~

和大家一起学习吧~

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn