How to remove an "item" from the last record?

Asked

Viewed 54 times

0

I need the last record not to show the <div class="staffClearFix"></div>

<?php 
    $userdata = mysql_query("SELECT * FROM users WHERE rank = '9' AND status = 'Ativo' ORDER BY id");
    while($row = mysql_fetch_assoc($userdata)){
?>
    <?php echo $row['username']; ?>
    <div class="staffClearFix"></div>   
<?php } ?>

1 answer

0


Invert the order and add a logic to delete from the first record that will be easier:

$first = true;
while($row = mysql_fetch_assoc($userdata)) {
   if (!$first) 
   {
       echo '<div class="staffClearFix"></div>';
   }

   echo $row['username'];

   $first = false;
}

Another detail, do not use functions mysql_* they are obsolete since version 5.5 and removed in PHP 7. Use Mysqli or PDO

  • But I need to remove the last, not to reverse :(

  • Have you tested the code? It will do exactly what you requested.

  • Sorry, I was kind of putting it wrong here... It did work! Could you explain to me how it works? I don’t quite understand how this code works, I’d like to learn.

  • @Carlosg will basically put the div always before the name, except if it is the first name. Then, in the first name, there is no div before the name. Now in the second name shall be the div before the name. And it shall be thus: (name | div | name). And so on.

Browser other questions tagged

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