如果数据库 table 在 cakephp 中为空,如何显示消息

how to display a message if database table is empty in cakephp

我可以使用

检索数据库条目

$this->set('view_applications',$this->Application->find('all'));

但我不知道如果上述函数的结果为空,如何显示消息。

这是我的控制器代码

public function index() {


    $this->set('results', $this->Application->find('all'));


}

这是我的 index.ctp 查看代码

    <h1>Requests for Graduation Registration</h1>
<table class="default-table full">
    <thead>
        <tr>

            <th style= text-align:center>Last Name</th>
            <th style= text-align:center>First Name</th>
            <th style= text-align:center>Middle Name</th>
            <th style= text-align:center>Computer Number</th>
             <th style= text-align:center>Status</th>
            <th style= "text-align:center; width: 40%;">Actions</th>
        </tr>
    </thead>
    <tbody>                     
        <?php $count=0; ?>
        <?php foreach($view_applications as $applications):     
        ?>

        <?php $count ++;?>
        <?php if($count % 2): echo '<tr>'; else: echo '<tr class="zebra">' ?>
        <?php endif; ?>

            <td style="text-align: center;"><?php echo $applications['Application']['last_name'];?> </td>
            <td style="text-align: center;"><?php echo $applications ['Application']['first_name']; ?></td>
            <td style="text-align: center;"><?php echo $applications ['Application']['middle_name']; ?></td>
            <td style="text-align: center;"><?php echo $applications['Application']['student_id']; ?></td>
            <td style="text-align: center;"><?php echo $applications['Application']['status']; ?></td>
        <td>
        <?php echo $this->Form->create('Student'); ?>
        <?php echo $this->Form->button(
    'View', 
    array(
        'formaction' => Router::url(
            array('controller' => 'Applications','action' => 'viewdown', $applications['Application']['id'])
         )
    )
); ?> |
<?php echo $this->Form->button(
    'Download', 
    array(
        'formaction' => Router::url(
            array('controller' => 'Applications','action' => 'viewdown', $applications['Application']['id'], true)
         )
    )
); ?> |



            <?php echo $this->Form->button(
    'Approve', 
    array(
        'formaction' => Router::url(
            array('controller' => 'Applications','action' => 'approve', $applications['Application']['id'])
         )
    )
); ?> |
<?php echo $this->Form->button(
    'Disapprove', 
    array(
        'formaction' => Router::url(
            array('controller' => 'Applications','action' => 'disapprove', $applications['Application']['id'])
         )
    )
); ?>










}


    </td>
        </tr>
        <?php endforeach; ?>
        <?php unset($applications); ?>
    </tbody>
</table>


</div>  
</body>

</html>

检查条件应该放在哪里?

你可以先数数。

`$count = $this->someModel->find('count'); // this display the amount of records, empty like 0.`

您在控制器中使用了 $this->set('results', $this->Application->find('all'));,但在您看来,您正在循环使用不同的变量。

"results" 应该是您在控制器中设置的 foreach 中的变量。

顺便说一句。要回答您的问题,您应该使用 count.

if(count($results) > 0){

   //Do whatever u like with data
}else{
 //Result is empty display a message
}