如何在codeigniter中加入2表

时间:2017-06-11 06:14:21

标签: php mysql codeigniter

数据存储在数据库中,但无法在页面中显示。请帮帮我

-table project

PROJECT_ID, 项目名, Project_Desc, USER_ID

-table user

User_ID,User_Name

这是我在模型中的代码

public function get_all_project()  { 

$this->db->select('*');
$this->db->from('user');
$this->db->join('project','project.User_ID = user.User_ID');
$query = $this->db->get();  
return $query->result();  

} 

这里是我在控制器中的代码

    public function list_all_project() {

    $data['projectadmin_list'] = $this->projectadmin_model->get_all_project();
    $this->load->view('projectadmin_list',$data);
    $this->load->model('projectadmin_model');
  }

这里我的代码在视图中

    <?php 

        foreach ($projectadmin_list as $data){ ?> 

     <tr> 
      <td><?php echo $data->Project_ID; ?></td> 
      <td><?php echo $data->Project_Name; ?></td>  
      <td><?php echo $data->Project_Desc; ?></td>
      <td><?php echo $data->Project_Total; ?></td>
      <td><?php echo $data->User_ID; ?></td>

      <td width="60" align="left" ><a href="#" onClick="show_confirm('edit',<?php echo $data->Project_ID;?>)">Edit</a></td>
      <td width="60" align="left" ><a href="#" onClick="show_confirm('delete_project',<?php echo $data->Project_ID;?>)">Delete </a></td>

     </tr>  
    <?php }?>  

4 个答案:

答案 0 :(得分:1)

在使用其方法之前加载模型

public function list_all_project() {
    $this->load->model('projectadmin_model'); // should be load here
    $data['projectadmin_list'] = $this->projectadmin_model->get_all_project();
    $this->load->view('projectadmin_list',$data);

  }

答案 1 :(得分:0)

首先加载模型,然后使用它:

public function list_all_project() {
    $this->load->model('projectadmin_model');
    $data['projectadmin_list'] = $this->projectadmin_model->get_all_project();
    $this->load->view('projectadmin_list',$data);
}

答案 2 :(得分:0)

如果您在控制器脚本中没有加载模型,请先加载它

function __construct()

{
    parent::__construct();
    $this->load->model('Model_File');
}

//尝试此查询。

public function get_all_project()

{   
    $this->db->select('*');
    $this->db->from('project');     
    $this->db->join('user','user.User_ID = project.User_ID');       
    $query = $this->db->get()->result();
    return $query;

}

答案 3 :(得分:0)

Try this one:

 Controller:

public function __construct()
    {
          parent::__construct();
          $this->load->model('projectadmin_model');
    }

public function list_all_project()
 {

   $data['projectadmin_list'] =   this->projectadmin_model->list_all_project();
   $this->load->view('projectadmin_list',$data);

 }




Model:    

 public function __construct()
    {
      $this->load->database();
    }

function list_all_project()
   {
      $this->db->select('*');
      $this->db->from('project p');
      $this->db->join('user u', 'u.User_ID = p.User_ID');       
      $query=$this->db->get();
      return $query->result_array();
  }