使用php在while循环中分页

使用php在while循环中分页,php,mysql,pagination,Php,Mysql,Pagination,我是php新手,正在努力学习。 我搜索了有关while循环分页的其他主题,但不满意 我的数据库中有70条用户记录,希望用html表列出它们。我正在显示带有此代码的所有记录。如何使用这些代码进行简单的分页?请帮帮我 <table class="table"> <thead> <tr> <th>ID</th> <th>Name</th> <th

我是php新手,正在努力学习。 我搜索了有关while循环分页的其他主题,但不满意

我的数据库中有70条用户记录,希望用html表列出它们。我正在显示带有此代码的所有记录。如何使用这些代码进行简单的分页?请帮帮我

<table class="table">
    <thead>
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Surname</th>
        <th>Email</th>
        <th>Password</th>
        <th>Date</th>
        <th>Gender</th>
      </tr>
    </thead>
    <tbody>

    <?php
    $q = "SELECT * FROM users ORDER BY uid ASC";
    $r = mysqli_query($dbc,$q);

    while($userlist = mysqli_fetch_assoc($r)){ ?>
      <tr>
          <td><?php echo $userlist['uid']; ?></td>
          <td><?php echo $userlist['name']; ?></td>
          <td><?php echo $userlist['surname']; ?></td>
          <td><?php echo $userlist['email']; ?></td>
          <td><?php echo $userlist['password']; ?></td>
          <td><?php echo $userlist['date']; ?></td>
          <td><?php echo $userlist['gender']; ?></td>
      </tr>
      <?php } ?>     
    </tbody>
  </table>

身份证件
名称
姓
电子邮件
密码
日期
性别

以下内容提供了非常简单的分页作为起点。您需要为分页等提供格式

<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Password</th>
            <th>Date</th>
            <th>Gender</th>
        </tr>
    </thead>
    <tbody>
<?php
    $q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
    $c = mysqli_query($dbc,$q);
    if($c) {
        if($t = mysqli_fetch_assoc($c)) {
            $numrows = $t['numrows'];
        }
    }

    $numrows = 0;
    $rowsperpage = 10;
    $currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
    $numpages = ceil($numrows / $rowsperpage);
    $startrow = ($currpage - 1) * $rowsperpage;
    if($startrow > $numrows) {
        $startrow = $numrows - $rowsperpage;
    }
    if($startrow < 0) {
        $startrow = 0;
    }


    $q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
    $r = mysqli_query($dbc,$q);

    while($userlist = mysqli_fetch_assoc($r)){ 
?>
        <tr>
                <td><?php echo $userlist['uid']; ?></td>
                <td><?php echo $userlist['name']; ?></td>
                <td><?php echo $userlist['surname']; ?></td>
                <td><?php echo $userlist['email']; ?></td>
                <td><?php echo $userlist['password']; ?></td>
                <td><?php echo $userlist['date']; ?></td>
                <td><?php echo $userlist['gender']; ?></td>
        </tr>
        <?php } ?>       
    </tbody>
</table>
<div id='pagination'>
<?php
    for($pgno = 1;$pgno <= $numpages;$pgno++) {
        echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
    }
?>
</div>

身份证件
名称
姓
电子邮件
密码
日期
性别

以下内容提供了非常简单的分页作为起点。您需要为分页等提供格式

<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Name</th>
            <th>Surname</th>
            <th>Email</th>
            <th>Password</th>
            <th>Date</th>
            <th>Gender</th>
        </tr>
    </thead>
    <tbody>
<?php
    $q = "SELECT count(*) as `numrows` FROM `users` ORDER BY `uid` ASC";
    $c = mysqli_query($dbc,$q);
    if($c) {
        if($t = mysqli_fetch_assoc($c)) {
            $numrows = $t['numrows'];
        }
    }

    $numrows = 0;
    $rowsperpage = 10;
    $currpage = isset($_REQUEST['currpageno']) && $_REQUEST['currpageno'] != 0 ? $_REQUEST['currpageno'] : 1;
    $numpages = ceil($numrows / $rowsperpage);
    $startrow = ($currpage - 1) * $rowsperpage;
    if($startrow > $numrows) {
        $startrow = $numrows - $rowsperpage;
    }
    if($startrow < 0) {
        $startrow = 0;
    }


    $q = "SELECT * FROM `users` ORDER BY `uid` ASC LIMIT ".$startrow.",".$rowsperpage.";";
    $r = mysqli_query($dbc,$q);

    while($userlist = mysqli_fetch_assoc($r)){ 
?>
        <tr>
                <td><?php echo $userlist['uid']; ?></td>
                <td><?php echo $userlist['name']; ?></td>
                <td><?php echo $userlist['surname']; ?></td>
                <td><?php echo $userlist['email']; ?></td>
                <td><?php echo $userlist['password']; ?></td>
                <td><?php echo $userlist['date']; ?></td>
                <td><?php echo $userlist['gender']; ?></td>
        </tr>
        <?php } ?>       
    </tbody>
</table>
<div id='pagination'>
<?php
    for($pgno = 1;$pgno <= $numpages;$pgno++) {
        echo "<a class='' href='?currpageno=".$pgno."'>".$pgno."</a>";
    }
?>
</div>

身份证件
名称
姓
电子邮件
密码
日期
性别


使用LIMIT子句并维护要在页面中显示和开始记录的数字。查询中需要
LIMIT
OFFSET
偏移量
是根据当前页面和
限制
的设置动态生成的。因此
LIMIT 3
每页提供3个项目。然后需要将
OFFSET
设置为
($pageNumber-1)*3
,使其在查询中成为
OFFSET($pageNumber-1)*3
。请记住验证页码,因为它们通常可以被用户操纵-使用带有占位符的准备好的语句。作为建议:不要以明文形式存储密码,也不要向某人显示所有用户的密码。我如何才能做到这一点$q=“按uid ASC LIMIT 10从用户订单中选择*”;它给我10条记录,我如何用按钮(分页)显示其他记录,偏移量是多少?我怎么用这个?请解释一下example@NihatÖzyedi也许你会加密密码?!以用户应得的安全对待他们;总是散列(不是加密,散列!)密码和其他敏感信息,并且从不向任何人显示。像这样主动地显示和不安全地存储它是一个很大的安全问题,如果我知道一个网站正在这样做,我永远不会使用它并主动警告其他人不要使用它。尊重和安全地对待用户提供的信息!如果您的帐户被黑客入侵,那么有人可以访问adminpanel并查看每个人的密码!请参阅使用限制子句并维护要在页面中显示和开始记录的数字。查询中需要
限制
偏移量
偏移量
是根据当前页面和
限制
的设置动态生成的。因此
LIMIT 3
每页提供3个项目。然后需要将
OFFSET
设置为
($pageNumber-1)*3
,使其在查询中成为
OFFSET($pageNumber-1)*3
。请记住验证页码,因为它们通常可以被用户操纵-使用带有占位符的准备好的语句。作为建议:不要以明文形式存储密码,也不要向某人显示所有用户的密码。我如何才能做到这一点$q=“按uid ASC LIMIT 10从用户订单中选择*”;它给我10条记录,我如何用按钮(分页)显示其他记录,偏移量是多少?我怎么用这个?请解释一下example@NihatÖzyedi也许你会加密密码?!以用户应得的安全对待他们;总是散列(不是加密,散列!)密码和其他敏感信息,并且从不向任何人显示。像这样主动地显示和不安全地存储它是一个很大的安全问题,如果我知道一个网站正在这样做,我永远不会使用它并主动警告其他人不要使用它。尊重和安全地对待用户提供的信息!如果您的帐户被黑客入侵,那么有人可以访问adminpanel并查看每个人的密码!看到为什么投票被否决了吗?还有,为什么投票时不留下任何关于原因的评论呢?解析错误:语法错误,在“给出此错误消息”中出现意外的文件结尾“我想是在什么地方失踪了。在代码中的某个地方尝试。什么是currpageno?您可以将其作为变量进行请求。它是否意味着像index.php这样的动态网址?page=7?编辑:我正在使用引导分页。像这样使用代码
    因为它不在您提供的代码中,也没有任何指示,所以我提供了一个非常简化的分页作为示例。而是关注sql语句,以及它如何根据要显示的页面限制查询中的行。我试图关注这个“request currpagno”,但不理解,仍然缺少“}”。谢谢,我已经修复了丢失的}。关于$\u请求['currpageno']。这就是代码知道显示哪一页结果的方式。页码在页码标签中。为什么投反对票?还有,为什么投票时不留下任何关于原因的评论呢?解析错误:语法错误,在“给出此错误消息”中出现意外的文件结尾“我想是在什么地方失踪了。在代码中的某个地方尝试。什么是currpageno?您可以将其作为变量进行请求。它是否意味着像index.php这样的动态网址?page=7?编辑:我正在使用引导分页。像这样使用代码
      因为它不在您提供的代码中,也没有任何指示,所以我提供了一个非常简化的分页作为示例。焦点研究所