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

708 views Asked by At

i can retrieve database entries with

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

but i do not know how to display a message if the results of the above function is empty.

here is my controller code

public function index() {


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


}

and here is my index.ctp view code

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

Where should i place the condition for checking?

2

There are 2 answers

0
Michel On BEST ANSWER

You used $this->set('results', $this->Application->find('all')); in your controller, and in your view you are looping with a different variable.

"results" should be your variable in foreach as set in your controller.

That's just by the way. To answer your question, you should use count.

if(count($results) > 0){

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

You can count first.

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