dongmubi4444
2019-04-16 09:10 阅读 11
已采纳

如果表是空的,如何显示'No Record found'?

I am new in cakephp 3. Now, I am fetching data from database which is successfully show all table data but I want to show 'No record found' message if table is empty. So, How can I do this ?Please help me.

Controller: PostsController

<?php
    namespace App\Controller;
    use App\Controller\AppController;

    Class PostsController extends AppController {
        Public function index(){
            $this->set('data',$this->Posts->find('all'));
        }
    }
?>

Layout: Index.cpt

<div class="row">
    <table class="table">
        <thead>
            <tr>
                <th>Title</th>
                <th>Description</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php
                if(!empty($data))
                {
                    foreach($data as $row)
                    {
            ?>
                        <tr>
                            <td><?php echo $row->title; ?></td>
                            <td><?php echo $row->description; ?></td>
                            <td></td>
                        </tr>
            <?php
                    }
                }
                else
                {
                    echo '<p>No record found</p>'; 
                }
            ?>
        </tbody>
    </table>
</div>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dsghpgmay31938863 dsghpgmay31938863 2019-04-16 09:13

    As find('all') method in cakephp return object, it wont be empty if check normally.

    Instead of

    if(!empty($data))
    

    Try this line

    if (!$data->isEmpty()) {
    

    Should work.

    Please find updated documentation link.

    点赞 评论 复制链接分享

相关推荐