查看来自codeigniter连接表的数据

时间:2016-06-28 14:43:00

标签: php mysql codeigniter

我想查看来自数据库的数据。有三个表要加入。两个表有相同的字段名称。当我尝试按如下方式查看数据时。但是出现了一个名为

的错误
  

遇到PHP错误

     

严重性:注意

     

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

     

文件名:views / boq_doc.php

     

行号:12

控制器

class Project_list extends CI_Controller {

function __construct(){
parent::__construct();
$this->load->model('project_list_model');
}
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);
}

}

模型

class Project_list_model extends CI_Model {


// Function To Fetch All Students Record
function show_projects(){

$this->db->select("project.project_name AS project_name1 , project.id AS 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();
}
// Function To Fetch Selected Student Record
function show_project_id($data){
$this->db->select('*');
$this->db->from('project');
$this->db->where('id', $data);
$query = $this->db->get();
$result = $query->result();
return $result;
}

}

查看

<?php foreach ($projects as $project): ?>
<li><a href="<?php echo base_url() . "index.php/project_list/show_project_id/" . $project->id; ?>"><?php echo $project->project_name1; ?></a></li>
<?php endforeach; ?>
</ol>
</div>
<div id="detail">
<!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
<?php foreach ($single_project as $project): ?>

<form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
<label id="hide">Id :</label>
<?php echo $project->project_name1; ?>
<label>Name :</label>
<input type="text" name="dname" value="<?php echo $project->location; ?>">
<label>Email :</label>
<input type="text" name="demail" value="<?php  echo $project->start_date; ?>">
<label>Mobile :</label>
<input type="text" name="dmobile" value="<?php  echo $project->end_date; ?>">
<label>Address :</label>
<input type="text" name="dmobile" value="<?php //  echo $project->firstname1; ?>">
<input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
<input type="submit" id="submit" name="dsubmit" value="Update">
</form>
<?php endforeach; ?>
</div>

1 个答案:

答案 0 :(得分:0)

你的foreach循环语法似乎错了试试这个

 <?
    foreach ($projects as $project)
    { 
    ?>
        <li><a href="<?php echo base_url()."index.php/project_list/show_project_id/" . $project->id; ?>"><?php echo $project->project_name1; ?></a></li>
<?
}
?>
        </ol>
        </div>
        <div id="detail">
        <!-- Fetching All Details of Selected Student From Database And Showing In a Form -->
        <?php foreach ($single_project as $project){ ?>

        <form method="post" action="<?php echo base_url() . "index.php/update_ctrl/update_project_id1"?>">
        <label id="hide">Id :</label>
        <?php echo $project->project_name1; ?>
        <label>Name :</label>
        <input type="text" name="dname" value="<?php echo $project->location; ?>">
        <label>Email :</label>
        <input type="text" name="demail" value="<?php  echo $project->start_date; ?>">
        <label>Mobile :</label>
        <input type="text" name="dmobile" value="<?php  echo $project->end_date; ?>">
        <label>Address :</label>
        <input type="text" name="dmobile" value="<?php //  echo $project->firstname1; ?>">
        <input type="text" name="daddress" value="<?php // echo $project->project_address; ?>">
        <input type="submit" id="submit" name="dsubmit" value="Update">
        </form>
        <?
        }

        ?>
        </div>