温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用php实现分页功能

发布时间:2021-09-10 17:56:58 来源:亿速云 阅读:84 作者:chen 栏目:开发技术

本篇内容主要讲解“怎么用php实现分页功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用php实现分页功能”吧!

复制代码 代码如下:

<?php     header("content-type:text/html;charset=utf-8");    //数据库连接    $conn = mysql_connect("localhost", "root", "111") or die("not connnected : ".mysql_error());    mysql_select_db("test", $conn);    mysql_query("set names utf8");
    //查询共有多少行数据    $sql1 = "select count(*) from user";    $ret1 = mysql_query($sql1);    $row1 = mysql_fetch_row($ret1);    $tot = $row1[0];
    //每页多少行数据    $length = 5;          //总页数      $totpage = ceil($tot / $length);
    //当前页数    $page = @$_GET['p'] ? $_GET['p'] : 1;    //limit 下限    $offset = ($page - 1) * $length;
    echo "<center>";    echo "<h3>php padding</h3>";    echo "<table width='700px' border='1px' >";    echo "<tr>";    echo "<th>ID</th>";    echo "<th>USER</th>";    echo "<th>PASS</th>";    echo "</tr>";
    //将查询出来的数据用表格显示    $sql2 = "select * from user order by id limit {$offset}, {$length}";    $ret2 = mysql_query($sql2);    while ($row2 = mysql_fetch_assoc($ret2)) {        echo "<tr>";        echo "<td>{$row2['id']}</td><td>{$row2['name']}</td><td>{$row2['pass']}</td>";        echo "</tr>";    }
    echo "</table>";
    //上一页和下一页    $prevpage = $page - 1;    if ($page >= $totpage) {        $nextpage = $totpage;    } else {        $nextpage = $page + 1;    }
    //跳转    echo "<h4><a href='index.php?p={$prevpage}'>上一页</a>|<a href='index.php?p={$nextpage}'>下一页</a></h4>";    echo "</center>";

核心点:

<1>“$sql2 = "select * from user order by id limit {$offset}, {$length}";”,$offset、$length和页数之间的关系。

<2>上一页和下一页的获得方式,以及临界点。

到此,相信大家对“怎么用php实现分页功能”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI