Hello. There is an array in the $myMessage['viewed'] array in it for the elements that have the ['status'] key, which interests me. Everything works ok, only if I have for example 3 messages with ['status'] == 0 gives me NEW NEW NEW

The task of each item to check the status, and if at least one of them has zero, then you need to output one NEW. How can this be done without interfering with SQL?

 <?php foreach ($myMessage['viewed'] as $item) { ?> <?php if( $item['status'] == 0) { ?> <?php $checkByStatus = true; ?> <?php if( $checkByStatus == true) { ?> chek = <?php echo $checkByStatus; ?> <span style="color: red;">NEW</span> <?php }?> <?php } ?> <?php }?> 
  • one
    The output of NEW should be after the loop, before the loop, make the variable $ hasNewMessage = false; and change its value to true in inside if. - fens
  • And make a bryak inside if If you have at least once a condition met, you need to display NEW and for this you do not need to run through the other n records. - fens
  • @fens The first option did not help. It was decided through break. - ultimatum
  • one
    The first option is how to write readable code, find what you have inside if the variable is set to true, which is why it is checked for truth ... - fens

1 answer 1

Well, interrupt the loop with break after the first NEW output.

  • All ingenious is simple. Thank. - ultimatum