Only first row of table being displayed!

Asked

Viewed 403 times

1

I’m requesting the table data this way:

    $sql = "SELECT * FROM `login` order by `userid` DESC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
    $userid = $sql['userid'];
    $sex = $sql['sex'];
    $email = $sql['email'];
    $group_id = $sql['group_id'];
    $last_ip = $sql['last_ip'];
}
echo '
<table class="vertical-table th">
    <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
    </tr>
<tr align="center">
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
    </tr>
    </table>

Testing the query directly in the database works perfectly, but the page only returns the first line...

3 answers

3


It is only display the values of the last record because its echo is after the while, If you want to print all the records throw the code inside the loop.

while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
}
echo '
//... mais código
<p style="margin-top: 0; margin-bottom: 0">
<font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>

Can fix when interpolating PHP with HTML:

<table class="vertical-table th">
    <tr align="center">
      <th width="10%">ID/th>
      <th width="10%">Login</b>/th>
    </tr>

<?php
    $sql = "SELECT * FROM `login` order by `userid` DESC";
    $limite = mysqli_query($db, $sql);
    while ($sql = mysqli_fetch_array($limite)) {
       $account_id = $sql['account_id'];
       $userid = $sql['userid'];
?>
       <tr align="center">
          <td><?php echo $account_id; ?></td>
          <td><?php echo $userid; ?></td>
       </tr> 
<?php 
    } 
?>
</table>
  • I didn’t understand the difference, I could illustrate a little better?

  • @Guilherme responding in a very practical way, you can find great examples in this documentation: http://php.net/manual/en/tutorial.php If you can not find here: http://learnphp.com.br/. Even so I know it’s a little advanced documentation but I think you’ll be able to understand how a While works

  • Perfect! Now I understand xD Thank you so much for the reply!

  • 1

    @William, his echo that prints the table is outside the while that is to say it will run only once different from the commands within the while that will run several times.

3

To use all the data you see from the Database you have to print it in all the iterations of the while, that is, while there are lines coming from the Database you print the table lines in HTML.

echo '
<table class="vertical-table th">
    <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
    </tr>
';    

$sql = "SELECT * FROM `login` order by `userid` DESC";
$limite = mysqli_query($db, $sql);
while ($sql = mysqli_fetch_array($limite)) {
    $account_id = $sql['account_id'];
    $userid = $sql['userid'];
    $sex = $sql['sex'];
    $email = $sql['email'];
    $group_id = $sql['group_id'];
    $last_ip = $sql['last_ip'];

    echo ' 
    <tr align="center">
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
      <td>
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
    </tr>
   ';
}   
echo'</table>';
  • You should follow @rray’s response to PHP interpolation with HTML. It gets simpler and easier to see where we actually do something dynamic.

  • Understood. But both answers were perfect thank you very much!

2

The while is the command that goes through the records of your query. You need it in the specific part of table that you want to repeat, in your case, would look something like:

echo '
<table class="vertical-table th">
   <tr align="center">
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>ID</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Login</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Sexo</b></font></th>
      <th width="10%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Email</b></font></th>
      <th width="25%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>Level</b></font></th>
      <th width="20%">
      <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>IP</b></font></th>
   </tr>
   ';

   while ($sql = mysqli_fetch_array($limite)) {
      $account_id = $sql['account_id'];
      $userid = $sql['userid'];
      $sex = $sql['sex'];
      $email = $sql['email'];
      $group_id = $sql['group_id'];
      $last_ip = $sql['last_ip'];

      echo '
      <tr align="center">
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#009900" size="2">'.$account_id.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2"><b>'.$userid.'</b></font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$sex.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" size="2">'.$email.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#E41B17" size="2">'.$group_id.'</font></p></td>
         <td>
         <p style="margin-top: 0; margin-bottom: 0"><font face="Verdana" color="#000099" size="2">'.$last_ip.'</font></p></td>
      </tr>
      ';
   }
echo '</table>';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.