如何在视图CodeIgniter中加载视图

时间:2015-03-10 13:38:32

标签: php codeigniter

控制器方法view()

function view() {
    $data['myClass'] = $this;
    $query = $this->db->query("Select * from news");
    if($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $data['news'][] = $row;
        }
    }

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

查看

<marquee onmouseover="this.stop();" onmouseout="this.start();" scrollamount="5" behavior="scroll"  direction="left">
    <?php
    $myClass->method('view');
    /*echo "<pre>";
    die(print_r($data, TRUE));*/

    foreach($news as $news) { ?>
        <p id="marque"><?php echo $news->description; ?></p>
    <?php } ?>
</marquee>

我的默认控制器在家,我想在家庭视图中加载此视图 但他们给我错误的未定义的foreach参数 但是当我调用控制器功能视图时,它就可以了 我的问题是如何在家庭视图中加载此视图

我正在寻找与requestaction()相当的CakePHP。

1 个答案:

答案 0 :(得分:0)

您需要将其分解并将代码分离为MVC。试试这个:

控制器:

function controller_method() {
    $this->load->model("model_name");
    $data['news'] = $this->model_name->model_method();
    $this->load->view('news_view', $data);
}

型号:

function model_method() {
    $this->db->select("*");
    $this->db->from("news");
    $query = $this->db->get();
    if ($query->num_rows() > 0) {
        return $query->result();
    } else {
        return false;
    }
}

查看:

<marquee onmouseover="this.stop();" onmouseout="this.start();" scrollamount="5" behavior="scroll"  direction="left">
    <?php foreach ($news as $new) { ?>
        <p id="marque"><?= $new->description; ?></p>
    <?php } ?>
</marquee>