-2

我正在尝试在 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
        }
    }

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

提前致谢

4

2 回答 2

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())并将其发送到视图。视图循环查询并显示数据。

于 2012-12-13T17:50:26.387 回答
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'];
于 2012-12-13T18:14:51.697 回答