dongmubi4444 2019-04-16 09:10
浏览 15
已采纳

如果表是空的,如何显示'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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错
  • ¥15 换yum源但仍然用不了httpd
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal