0

我想将数据从模型传递到控制器然后查看此代码解释它。

该模型

public function get_all_college_name() {
    $q = $this -> db -> query('select * from college');
    if ($q -> num_rows() > 0) {
        foreach ($q->result() as $row) {
            $data[] = $row;
        }
        return $data;
    }
}

控制器代码

public function index() {
    $this -> load -> model('retriver_data');
    $data['rows'] = $this -> retriver_data -> get_all_college_name();

    $this -> load -> view('home', $data);

    //$this -> view_something('home', $data);
}

查看代码

<body>
    <?php
    foreach ($rows as $r) {
        echo $r -> name;
    }
    ?>
</body>

出现错误

遇到 PHP 错误

严重性:警告

消息:为 foreach() 提供的参数无效

文件名:views/home.php

行号:8

4

1 回答 1

0

对于 CI,在我的模型中,我通常使用:

if ($query->num_rows() == 0)
    return FALSE;

return $query->result_array();

那么如果你的观点你会在显示之前检查数组是否为假

<body>
    <?php if ( ! $rows) : ?>
        no data
    <?php else: ?>
        <?php foreach ($rows as $row): ?>
            <?php print $row->name; ?>
        <?php endforeach;?>
    <?php endif; ?>
</body>
于 2013-09-27T16:38:48.193 回答