如何在codeigniter中查看连接表中具有相同字段名称的数据

时间:2016-06-29 05:51:29

标签: php mysql codeigniter

我有3张表如下。我想加入他们并查看视图中的数据。在我的代码中有一个名为:

的错误
  

遇到PHP错误

     

严重性:注意

     

消息:未定义的属性:stdClass :: $ firstname1

     

文件名:views / boq_doc.php

     

行号:12

project table
id | staff_id | client_id | location

staff table
id | firstname | lastname | address

client table
client_id | firstname | lastname | address

我已经编写了如下模型来从数据库中获取表数据:

function show_projects(){

$this->db->select("project.project_name, project.id, client.firstname AS firstname1, client.lastname AS lastname1,staff.firstname AS firstname2, staff.lastname AS lastname2, project.location,project.category, project.start_date, project.end_date"); 
$this->db->from('project');
$this->db->join('client', 'project.client_id = client.client_id');
$this->db->join('staff', 'staff.id = project.staff_id');
$query = $this->db->get();
return $query->result();
}

我想查看数据,我在视图中有如下代码:

<?php foreach ($single_project as $project): ?>

<?php echo $project->location->location1; ?>

<?php  echo $project->firstname1; ?>

<?php  echo $project->lastname1; ?>

<?php  echo $project->firstname2; ?>

<?php  echo $project->lastname2; ?>
<?php endforeach; ?>

控制器

function show_project_id() {
$id = $this->uri->segment(3);
$data['projects'] = $this->project_list_model->show_projects();
$data['single_project'] = $this->project_list_model->show_project_id($id);
$this->load->view('boq_doc', $data);
}

1 个答案:

答案 0 :(得分:1)

在控制器代码中

$data['projects'] = $this->project_list_model->show_projects();

包含您的查询结果表单模型

所以在视图中你可以使用

<?php foreach ($projects as $project): ?>