简体   繁体   English

while循环不显示所有值

[英]While loop is not displaying all values

I am having this weird problem. 我有这个奇怪的问题。 There are seven admins: darth, jane, luke, najin, root, sam and sydney. 有七个管理员:darth,jane,luke,najin,root,sam和sydney。

CODE: 码:

    <table>
          <tr>
            <th style="text-align: left; width: 200px;">Username</th>
            <th colspan="2" style="text-align: left;">Action</th>
          </tr>
        <?php 

            $sql = "SELECT * FROM admins ORDER BY Admin_Username ASC";
            $result = mysqli_query($connection, $sql);
            $admin = mysqli_fetch_assoc($result);

            while($admin = mysqli_fetch_assoc($result)) { 
            ?>
          <tr>
            <td><?php echo ($admin["Admin_Username"]); ?></td>
            <td><a href="edit_admin.php?id=<?php echo urlencode($admin["id"]); ?>">Edit</a></td>
            <td><a href="delete_admin.php?id=<?php echo urlencode($admin["id"]); ?>" onclick="return confirm('Are you sure you want to delete this admin?');">Delete</a></td>
          </tr>
        <?php     
} 
?>
</table>

If I use ASC order, the first admin, darth is not displayed in the loop and if I use DESC order, the last admin, sydney doesn't show up. 如果我使用ASC命令,第一个管理员,darth不会显示在循环中,如果我使用DESC命令,则最后一个管理员,sydney不显示。 What could be the problem here? 这可能是什么问题?

Get rid of the first $admin = line. 摆脱第一个$admin =行。

Your loop will fetch all of them; 你的循环将获取所有这些; you don't need to fetch the first one separately (and if fact by doing so are skipping it, because your loop immediately fetches the second before the first one could be written out). 你不需要单独获取第一个(如果这样做的话就是跳过它,因为你的循环会在第一个被写出之前立即获取第二个)。

删除此行

 $admin = mysqli_fetch_assoc($result);//You already fetching the first result here

You have a redundant call to $admin = mysqli_fetch_assoc($result); 您有$admin = mysqli_fetch_assoc($result);的冗余调用$admin = mysqli_fetch_assoc($result); before the while loop which will cause you to skip the first row of the result. while循环之前,这将导致您跳过结果的第一行。 Just remove it and you should be fine. 只需删除它,你应该没事。

    <?php 

            $sql = "SELECT * FROM admins ORDER BY Admin_Username ASC";
            $result = mysqli_query($connection, $sql);    
            while($admin = mysqli_fetch_assoc($result)) { 
            ?>
          <tr>
            <td><?php echo ($admin["Admin_Username"]); ?></td>
            <td><a href="edit_admin.php?id=<?php echo urlencode($admin["id"]); ?>">Edit</a></td>
            <td><a href="delete_admin.php?id=<?php echo urlencode($admin["id"]); ?>" onclick="return confirm('Are you sure you want to delete this admin?');">Delete</a></td>
          </tr>
    <?php     
    } 
    ?>
$admin = mysqli_fetch_assoc($result);

这必须在while循环中所以你可以在上面descard /删除旧值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM