简体   繁体   中英

How to join 2 table in codeigniter

The data stores in database but it cannot display in page. Help me please

-table project

Project_ID, Project_Name, Project_Desc, User_ID

-table user

User_ID, User_Name

here is my code in model

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();  

} 

here my code in controller

    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');
  }

here my code in view

    <?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 }?>  

Load model before you use its method

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);

  }

First load the model, then use it:

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);
}

if You have not Loaded Model in controller script load it first

function __construct()

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

// try this query.

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;

}
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();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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