如何将对象数组从控制器传输到查看器codeigniter

时间:2014-11-24 08:28:53

标签: php codeigniter

所以这是我在控制器中的代码:

public function logged_in()
{
    if($this->input->post('log_out'))
    {
        $this->session->unset_userdata('username');
        redirect(base_url()."main_c");
    }

    if(!$this->session->userdata('username'))
    {
        redirect(base_url()."main_c/login");
    }

    $this->load->model('main_m');

    $data = $this->main_m->get_user();



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


}

这是我的型号代码:

public function get_user()
{
    $query = $this->db->get('user');


    return $query->result();



}

所以基本上我想做的是在查看器中创建一个循环并输出$ data中的每个对象。所以我的问题是:如何在查看器中传输数组以使我的查看器看起来像这样:

    foreach($data as $ob)
    {
        echo "username: ".$ob->username." ";
        echo "password: ".$ob->password;
        echo "</br>";
    }

2 个答案:

答案 0 :(得分:0)

您的控制器代码应为:

public function logged_in()
{
    if($this->input->post('log_out'))
    {
        $this->session->unset_userdata('username');
        redirect(base_url()."main_c");
    }

    if(!$this->session->userdata('username'))
    {
        redirect(base_url()."main_c/login");
    }

    $this->load->model('main_m');

    $data['data'] = $this->main_m->get_user();



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


}

答案 1 :(得分:0)

在控制器中执行类似的操作 - &gt;

$this->load->model('main_m');

$data['users'] = $this->main_m->get_user();

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

然后在视图中 - &gt;

foreach($users as $ob)
{
    echo "username: ".$ob->username." ";
    echo "password: ".$ob->password;
    echo "</br>";
}