如果在cakephp

时间:2015-06-08 17:59:16

标签: cakephp cakephp-2.3

我可以使用

检索数据库条目

$ 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>

我应该在哪里放置检查条件?

2 个答案:

答案 0 :(得分:1)

您在控制器中使用了$this->set('results', $this->Application->find('all'));,并且在视图中使用其他变量进行循环。

&#34; results&#34;应该是您在控制器中设置的foreach中的变量。

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

if(count($results) > 0){

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

答案 1 :(得分:0)

你可以先计算一下。

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