Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loop is not displaying all values

Tags:

php

while-loop

I am having this weird problem. There are seven admins: darth, jane, luke, najin, root, sam and 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. What could be the problem here?

like image 400
Yugal1458 Avatar asked Dec 20 '22 09:12

Yugal1458


2 Answers

Get rid of the first $admin = line.

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).

like image 83
Amber Avatar answered Jan 02 '23 18:01

Amber


remove this line

 $admin = mysqli_fetch_assoc($result);//You already fetching the first result here
like image 35
arif_suhail_123 Avatar answered Jan 02 '23 18:01

arif_suhail_123