skip to Main Content

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


  1. Th solution would be to use the ternary operator within the PHP code:

    <?php foreach ($items as $k => $v): ?>
        <tr>
            <td><?= $v['number'] ?></td>
            <td><?= $v['date_from'] ?></td>
            <td><?= $v['date_till'] ? $v['date_till'] : $v['date_till2'] ?></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; ?>
    
    Login or Signup to reply.
  2. Here’s a more readable example with additional testing and secure handling (untested):

    foreach ( $items as $item ) {
        // If date_till is empty, set to date_till2;
        if ( empty( $item['date_till'] ) ) {
            $item['date_till'] = $item['date_till2'];
        }
    
        // If date_till is still empty, then we've no date: set to "Unknown".
        if ( empty( $item['date_till'] ) ) {
            $item['date_till'] = 'Unknown';
        }
    
        echo '<tr>';
    
        printf( '<td>%s</td>', esc_html( $item['number'] ) );
        printf( '<td>%s</td>', esc_html( $item['date_from'] ) );
        printf( '<td>%s</td>', esc_html( $item['date_till'] ) );
        printf( '<td>%s</td>', esc_html( $item['dealer_title'] ) );
    
        printf( 
            '<td>%s%s</td>', 
            esc_html( $item['producer_title'] ), 
            esc_html( $item['sys_create'] ) 
        );
    
        printf( 
            '<td>%s%s</td>', 
            nl2br( esc_html( $item['producer_address'] ) ), 
            nl2br( esc_html( $item['sys_create_address'] ) ) 
        );
    
        printf(
            '<td>%s%s</td>',
            nl2br( esc_html( $item['offers_title'] ) ),
            nl2br( esc_html( $item['sfera'] ) )
        );
    
        printf( '<td>%s</td>', esc_html( $item['status'] ) );
    
        echo '</tr>';
    }
    

    Ternary operators are valuable for their conciseness, which in some cases is more readable, but in most cases reduces readability.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search