CakePHP if no data show message
I have the following in my view for a client that shows a list of appointments for that client:
<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>
I want to wrap the table in a if statement that checks if any appointments exist and if not then show the following instead:
<p>Client has no appointments. <?php echo $this->Html->l开发者_Go百科ink('Book Appointment', array(
'admin' => true,
'controller' => 'appointments',
'action' => 'add',
'?' => array('id' => $client['Client']['id']))
); ?></p>
How would I do this please?
Thanks
You can use an if
statement and empty
to determine whether or not the client has any appointments.
<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; ?>
精彩评论