php - 使用php在while循环中分页

标签 php mysql pagination

我是 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>

关于php - 使用php在while循环中分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43242713/

相关文章:

php - 将日志与弹性时间安排进行匹配

mysql - 外键在双向关系中的作用

php - 从 CodeIgniter 中每个页面所需的模型中检索数据的最佳方法是什么?

javascript - 获取特定行的属性值?

java - JSTL 帮助分页

php - 如何更改 !isset 语句

php - 使用 PHP 将 XML 转换为 CSV

php - 用数值存储在数据库描述中

php - 高效评论系统分页查询

c# - 如何在 ASP.NET 中使用 Repeater 控件进行分页?