![](/img/trans.png)
[英]How to pass view data to Controller by CodeIgniter using ajax Code?
[英]How to pass data controller to view in codeigniter
我正在从数据库中获取用户配置文件字段,并希望在我的视图中显示,但是不知道如何使用主布局进行显示。
这是我的模特
function fetchProfile($id,$page){
$query = $this->db->query("SELECT * FROM staff_master WHERE Id='$id'");
return $query->result();
}
这是我的控制器:
public function edit($id,$page){
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
$this->load->view('template_master',$data);
}
我也在尝试寻找解决方案。 我通过URL(get方法)传递用户ID和另一个。
当您接下来分配数组时,您正在覆盖$data['query']
:
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data= array('content'=>'view_staff_edit');
要么做:
$data= array('content'=>'view_staff_edit');
$data['query'] = $this->StaffModel->fetchProfile($id,$page); // note position
要么:
$data = array(
'content' = 'view_staff_edit',
'query' => $this->StaffModel->fetchProfile($id,$page),
);
通过$query
和$content
访问视图。
无关:
您还会在查询中丢失$page
,通常最好将其声明为null(如果未设置),否则您将得到通知: public function edit($id=null,$page=null){
覆盖变量$data
第一个声明的$data
是,可以同时初始化它们。
调节器
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id,$page),
'content' => 'view_staff_edit'
);
$this->load->view('template_master',$data);
}
然后在您的查看文件中访问它
<h1><?php echo $content ?></h1>
<?php foreach($query as $result): ?>
<p><?php echo $result->id ?></p>
<?php endforeach; ?>
尝试做这样的事情:
public function edit($id,$page) {
$data['query'] = $this->StaffModel->fetchProfile($id,$page);
$data['content']= 'view_staff_edit';
$this->load->view('template_master',$data);
}
您的模型
function fetchProfile($id){
return $this->db->get_where('Id' => $id)->result_array();
}
您的控制器
public function edit($id,$page){
$data = array(
'query' => $this->StaffModel->fetchProfile($id),
'content' => 'view_staff_edit',
);
$this->load->view('template_master',$data);
}
您的“ 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.