如何将变量从模型传递到控制器以在代码点火器中的视图上使用?

时间:2012-12-13 17:27:27

标签: php codeigniter variable-assignment

我正在尝试在codeigniter中创建我的用户模块,但无法弄清楚如何将模型中的查询结果传递给允许我在视图中使用的控制器。

这是我的模特:

public function user() 
        {
            $session = $this->session->userdata('is_logged_in');
            $user_id = $this->session->userdata('id');
            $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1"); 
            foreach ($query->result() as $row)
            {
                $row->first_name;
                $row->id;
                $row->email;
                $row->last_name;
            }

        }

这是我的控制器。怎么做我传递这些对象?或者我应该将它们放在一个数组中然后传递数组(仍然不知道如何传递它)然后从控制器上的数组中提取它们?

public function edit() 
    {
        $data['first_name'] = $this->session->userdata('first_name');
        $data['profile_icon'] = 'edit';
        $data['profile_breadcrumbs'] = 'Edit Profile';
        $data['main_content'] = 'account/edit';
        $this->load->view('includes/templates/main_page_template', $data);
        $this->load->library('session');
        $this->load->model('account_model');
        $session = $this->session->userdata('session_id');
        $this->account_model->user();
        if($session)
        {
            //TRYING TO GET OBJECTS
        }
    }

我发现了与此相关的类似问题,但很难找到我正在寻找的内容。

提前致谢

2 个答案:

答案 0 :(得分:2)

您必须将其分配给变量并使用它:

$user = $this->account_model->user();

foreach($user->result() as $row){
    echo $row->name;
}

如果您想将数据传递给视图,则必须确保在设置了所有$data后最后加载视图:

$user = $this->account_model->user();
$data['user'] = $user;
$this->load->view('includes/templates/main_page_template', $data);

然后,您可以通过$user变量在视图中使用模型生成的用户数据。


编辑:

还注意到你不是return来自模特的任何东西:/

型号:

public function user() 
    {
        $session = $this->session->userdata('is_logged_in');
        $user_id = $this->session->userdata('id');
        $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1"); 
        return $query;

    }

控制器:

$user = $this->account_model->user();
$data['user'] = $user;
$this->load->view('includes/templates/main_page_template', $data);

查看:

foreach($user->result() as $row){
    $first_name = $row->first_name;
    $id = $row->id;
    $email = $row->email;
    $last_name = $row->last_name;
}

echo "User id: $id has $first_name as a first name, $last_name as their last name, and $email as their email address";

因此,您的模型查询数据库,返回结果。控制器调用模型中的函数以获取它想要的数据(在本例中为user())并将其发送到视图。视图循环遍历查询并显示数据。

答案 1 :(得分:2)

控制器:

$data['user']=$this->account_model->user();
//populates $data['user'] with the user details based on the session.
$this->load->view('includes/templates/main_page_template', $data);

模特:

public function user() 
    {
        $session = $this->session->userdata('is_logged_in');
        $user_id = $this->session->userdata('id');
        $query = $this->db->query("SELECT * FROM users WHERE id=$user_id LIMIT 1");
        if($query->num_rows()==1)
        {
           $data = $query->result_array();
           return $data[0];
        //above returns the single row you need to the controller as an array.
        //to the $data['user'] variable.
        } else {
           do something here to handle no return
        }
    }

然后在视图中,您的数据可以通过$ user变量访问,因此您可以这样做:

echo 'Hi '.$user['first_name'];