How to make a condition in this construction – display one or another date?
<?php foreach ($items as $k => $v): ?>
<tr>
<td><?= $v['number'] ?></td>
<td><?= $v['date_from'] ?></td>
<td><?= $v['date_till'] ?></td>
<td><?= $v['dealer_title'] ?></td>
<td><?= $v['producer_title'] ?><?= $v['sys_create'] ?></td>
<td><?php echo nl2br($v['producer_address']); ?><?php echo nl2br($v['sys_create_address']); ?></td>
<td><?= nl2br($v['offers_title']) ?><?= nl2br($v['sfera']) ?></td>
<td><?= $v['status'] ?></td>
</tr>
<?php endforeach; ?>
The database has 2 tables: date_till and date_till2.
It is necessary that, if necessary, the date is displayed either from date_till or date_till2.
At the moment, if there is no data in the date_till table, an incorrect date is displayed: 30.11.-0001.
Thanks in advance for any help!
2
Answers
Th solution would be to use the ternary operator within the PHP code:
Here’s a more readable example with additional testing and secure handling (untested):
Ternary operators are valuable for their conciseness, which in some cases is more readable, but in most cases reduces readability.