如果没有数据显示消息,CakePHP

时间:2011-07-17 20:58:26

标签: php cakephp

我在我的视图中有以下内容,其中显示了该客户的约会列表:

<h3>Appointments</h3>

<table>
    <?php foreach ($client['Appointment'] as $appointment): ?>
        <tr>
            <td>
                <?php echo $this->Html->link($appointment['date'],
                array('admin' => true, 'controller' => 'appointment', 'action' => 'view', $appointment['id'])); ?>
            </td>
            <td>
                <?php echo $appointment['type']; ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

我想将表包装在if语句中,该语句检查是否存在任何约会,如果没有,则显示以下内容:

<p>Client has no appointments. <?php echo $this->Html->link('Book Appointment', array(
        'admin' => true,
        'controller' => 'appointments',
        'action' => 'add',
        '?' => array('id' => $client['Client']['id']))
    ); ?></p>

我该怎么办呢?

由于

1 个答案:

答案 0 :(得分:3)

您可以使用if语句和empty来确定客户是否有任何约会。

<h3>Appointments</h3>

<?php if ( ! empty($client['Appointment']) ): ?>
<table>
    <?php foreach ($client['Appointment'] as $appointment): ?>
        <tr>
            <td>
                <?php echo $this->Html->link($appointment['date'],
                array('admin' => true, 'controller' => 'appointment', 'action' => 'view', $appointment['id'])); ?>
            </td>
            <td>
                <?php echo $appointment['type']; ?>
            </td>
        </tr>
    <?php endforeach; ?>
</table>
<?php else: ?>
<p>Client has no appointments. <?php echo $this->Html->link('Book Appointment', array(
        'admin' => true,
        'controller' => 'appointments',
        'action' => 'add',
        '?' => array('id' => $client['Client']['id']))
    ); ?></p>
<?php endif; ?>