无法将视图另存为要在模板中使用的变量

时间:2012-07-17 20:03:13

标签: php codeigniter

我试图从我的控制器调用一个模型,然后控制器从视图中生成数据,将其保存为内容并将其返回给控制器,然后控制器将其添加到我拥有的模板中。

发生的事情是$content = $this->load->view('left/profile', $c_data);正在打印数据而不是将其保存在变量$content

这是我的代码:

控制器:

function index($page = '1-Welcome to')
{
        if (!$this->tank_auth->is_logged_in()) {
            //display non-login page
            redirect('/auth/login/');
        } else { 
    //user information
        $user['user_id'] = $this->tank_auth->get_user_id();
        $user['username'] = $this->tank_auth->get_username();
    //general page data
        $main['title'] = $this->page_model->make_title(ucfirst($page));
    //template data
        $template['head'] = $this->load->view('templates/head', $main, TRUE); 
    //get left content
        $c_data['make_profile'] = $this->left_model->make_profile($user);

    //combine into one variable
        $data['template'] = $template;
        $data['page'] = $page;
        $data['user'] = $user;
        $data['left'] = $c_data;
        print_r($data);
        $this->load->view('main_template', $data);
        }
}

专注于$c_data['make_profile'] = $this->left_model->make_profile($user);

以下是make_profile

public function make_profile($user)
{
    $user_id = $user['user_id'];
    $query = $this->db->query(" SELECT location FROM user_profiles AS up 
                                INNER JOIN avatars AS a
                                ON up.avatar_id = a.id
                                WHERE user_id='$user_id'");
    $c_data['avatar_loc'] = $query->row_array();
    $content = $this->load->view('left/profile', $c_data);
    $content .= "hello";
    return $content;
}

这是我的个人资料视图:

<div id="profile">
    <div id='avatar'><img src="<?php echo $avatar_loc['location'] ?>" alt="avatar_user"/></div>
    <div id="profile_pop"></div>
</div>

知道它为什么不起作用?感谢

1 个答案:

答案 0 :(得分:5)

以字符串形式从视图中返回数据:

$this->load->view('left/profile', $c_data, TRUE);

阅读here(页面底部)。