如果在Codeigniter中为null,则foreach()期望无法显示数据

I was expecting view to show message 'no data found' when my query is returning null value but im getting error message: Invalid argument supplied for foreach()

Here's the controller code:

public function index()
    {
        $ctki = $this->ctki_model->get_all_ctki_data();
        if ( !empty($ctki) ) {
            $this->data['ctkiData'] = $ctki;
        } else {
            $this->data['ctkiData'] = 'Tidak ada data CTKI.';
        }
        $this->load->view($this->layout, $this->data);
    }

Here's the view code:

<table class="table table-striped table-bordered table-hover table-condensed">
                <thead>
                <tr>
                    <th>No</th>
                    <th>Nama Lengkap</th>
                    <th>Jenis Kelamin</th>
                    <th>No KTP</th>
                    <th>No Passport</th>
                    <th>Kota</th>
                    <th>No Telepon</th>
                    <th>No HP</th>
                    <th>Aksi</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach($ctkiData as $row): ?>
                    <?php
                    // Link edit, hapus, cetak
                    $link_edit = anchor('program/administrasi/edit/'.$row->ID_CTKI, '<span class="glyphicon glyphicon-edit"></span>', array('title' => 'Edit'));
                    $link_hapus = anchor('program/administrasi/hapus/'.$row->ID_CTKI,'<span class="glyphicon glyphicon-trash"></span>', array('title' => 'Hapus', 'data-confirm' => 'Anda yakin akan menghapus data ini?'));
                    ?>
                    <tr>
                        <td><?php echo ++$no ?></td>
                        <td><?php echo $row->Username ?></td>
                        <td><?php echo $row->Nama_Lengkap_User ?></td>
                        <td><?php echo $row->No_Telepon ?></td>
                        <td><?php echo $row->No_HP ?></td>
                        <td><?php echo $row->Level ?></td>
                        <td><?php echo format_is_blokir($row->Status_Blokir) ?></td>
                        <td><?php echo $link_edit.'&nbsp;&nbsp;&nbsp;&nbsp;'.$link_hapus ?></td>
                    </tr>
                <?php endforeach ?>
                </tbody>
            </table>

With

$this->data['ctkiData'] = 'Tidak ada data CTKI.';

you assign a string to this variable. In your template you expect it to be an array.

You can solve this on different ways, for instance by checking in your template if the data is an array and if it has got any items. If not show an error.

<?php 
if (is_array($ctkiData) && count($ctkiData) > 0) {
foreach($ctkiData as $row): ?>
table here
<?php endforeach } else { ?>
error message here
<?php } ?>

(not tested, but something like that).

A better solution is to use a template engine like Twig for instance and not to use php code in your html template.