Display a foreach item only once (if this item is within the if condition)

Asked

Viewed 548 times

0

Hello!
For a chat, I want to display the date of the message only once, this being the first message only.
Example:

ONTEM Mensagem 1 Mensagem 2 Mensagem 3

HOJE Mensagem 1 Mensagem 2 Mensagem 3

Today, what I have is working like this:

ONTEM Mensagem 1 ONTEM Mensagem 2 ONTEM Mensagem 3

HOJE Mensagem 1 HOJE Mensagem 2 HOJE Mensagem 3

Today I’m using the following code:

<?php
$dt_cadastro = strtotime($linha['dt_cadastro']);
$hoje_inicio = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
$hoje_fim = mktime(23, 59, 59, date('m'), date('d'), date('Y'));
$ontem_inicio = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
$ontem_fim = mktime(23, 59, 59, date('m'), date('d') - 1, date('Y'));
$anteontem_inicio = mktime(0, 0, 0, date('m'), date('d') - 2, date('Y'));
$anteontem_fim = mktime(23, 59, 59, date('m'), date('d') - 2, date('Y'));
?>
<?php if (($dt_cadastro >= $hoje_inicio) AND ($dt_cadastro <= $hoje_fim)): ?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">                                                                    
<span class=""><?= 'HOJE' ?></span>
</span>
<?php elseif (($dt_cadastro >= $ontem_inicio) AND ($dt_cadastro <= $ontem_fim)): ?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">
<span class=""><?= 'ONTEM' ?></span>
</span>
<?php elseif (($dt_cadastro >= $anteontem_inicio) AND ($dt_cadastro <=  anteontem_fim)):
?>
<span class="chat-data" style="position:absolute; left:45%; padding-bottom: 0px;">                                                                    
<span class=""><?= 'ANTEONTEM' ?></span>
</span>
<?php endif; ?>

This is inside a foreach

<?php foreach ($historico as $linha):?><br>
<?php endforeach;?>

PS: If there is another alternative to make code cleaner and less repetitive, I would like you to help me too, please.

  • You could not who knows create an array for each day? Then when the time fits in your condition the information is saved in the array Today, for example, and when displaying the messages you only write the word once Today and makes a loop to bring all messages within that array

  • @R.Santos, I have no idea how it would work.

  • I don’t know much about PHP @Wagnerfilho, but how are you saving the content of these variables? What kind of variable is this?

  • If you are referring to date variables, I am not saving, I am only using in order to compare to the date I am receiving from the database

  • Put the contents of the $historico array to your question

  • @Claydersonferreira, you don’t need it. It was solved in the way that Thiago Santos proposed.

Show 1 more comment

1 answer

2


First declares an auxiliary variable before the foreach: $hoje1 = 0;. Then you change the way it should work:

<?php if($hoje1 == 0) { $hoje1 = 1; ?><span class="">HOJE</span><?php } ?>

Does the same for yesterday and the day before yesterday.

(I changed the word today to HTML)

  • Dude, show.. it worked!! Thanks!

  • <?php if($hoje == 0) { $hoje = 1; ?><span style="position:absolute; left:45%; padding-bottom: 0px;" class="chat-data">HOJE</span><?php } ?>

Browser other questions tagged

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