简体   繁体   English

如何通过数据控制器在Codeigniter中查看

[英]How to pass data controller to view in codeigniter

I am getting user profile fields from the database and want to display in my view but don't know to do this with the master layout. 我正在从数据库中获取用户配置文件字段,并希望在我的视图中显示,但是不知道如何使用主布局进行显示。

this is my model 这是我的模特

function fetchProfile($id,$page){
 $query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
 return $query->result(); 
}

this is my controller: 这是我的控制器:

public function edit($id,$page){
    $data['query'] =  $this->StaffModel->fetchProfile($id,$page);
    $data= array('content'=>'view_staff_edit');
    $this->load->view('template_master',$data);
}

I am also trying to find a solution. 我也在尝试寻找解决方案。 I am passing user Id and another by URL (get method). 我通过URL(get方法)传递用户ID和另一个。

You are overwriting $data['query'] when you assign the array next: 当您接下来分配数组时,您正在覆盖$data['query']

$data['query'] =  $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');

Either do: 要么做:

$data= array('content'=>'view_staff_edit');
$data['query'] =  $this->StaffModel->fetchProfile($id,$page); // note position

Or: 要么:

$data = array(
    'content' = 'view_staff_edit',
    'query' => $this->StaffModel->fetchProfile($id,$page),
);

Access in view via $query and $content . 通过$query$content访问视图。

Unrelated: 无关:

You are also missing $page in your query, and its generally a good idea to declare gets as null if not set or you will get a notice: public function edit($id=null,$page=null){ 您还会在查询中丢失$page ,通常最好将其声明为null(如果未设置),否则您将得到通知: public function edit($id=null,$page=null){

Your overriding your first declaration of variable $data what you can do is to initialize them both at the same time. 覆盖变量$data第一个声明的$data是,可以同时初始化它们。

Controller 调节器

public function edit($id,$page){
    $data = array(
        'query'   => $this->StaffModel->fetchProfile($id,$page),
        'content' => 'view_staff_edit'
    );
    $this->load->view('template_master',$data);
}

Then access it on your View file 然后在您的查看文件中访问它

<h1><?php echo $content ?></h1>

<?php foreach($query as $result): ?>

    <p><?php echo $result->id ?></p>

<?php endforeach; ?>

Try doing something like this: 尝试做这样的事情:

public function edit($id,$page) {

    $data['query'] =  $this->StaffModel->fetchProfile($id,$page);
    $data['content']= 'view_staff_edit';
    $this->load->view('template_master',$data);

}

YOUR MODEL 您的模型

function fetchProfile($id){
    return $this->db->get_where('Id' => $id)->result_array(); 
}

YOUR CONTROLLER 您的控制器

public function edit($id,$page){
$data = array(
    'query'   => $this->StaffModel->fetchProfile($id),
    'content' => 'view_staff_edit',   
);
$this->load->view('template_master',$data);
}

YOUR "template_master" VIEW 您的“ template_master”视图

<?php foreach ($query as $res){?>
<span><?php echo $res['Id'];?></span> //User html content according to your requirements ALSO you can add all columns retrieved from database
<?php } ?> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM