Codeigniter Join 2 Table Data

codeigniter join 2 table data

It doesn't matter what table is first... Simply:

<?php

$this->db->select('t1.name, t2.something, t3.another')
->from('table1 as t1')
->where('t1.id', $id)
->join('table2 as t2', 't1.id = t2.id', 'LEFT')
->join('table3 as t3', 't1.id = t3.id', 'LEFT')
->get();
?>

Codeigniter join two tables in model

You should try this one.

$this->db->select('*')
->from('lists')
->join('list_items', 'list_items.list_id = lists.id')
->where('list_id', $id);

$query = $this->db->get();

Reference

  • query_builder

CodeIgniter join two tables

Try this in controller and see whats the result.Also,show us if you are getting any error and make sure there is data in your table :).

Controller

function getall(){      
$this->load->model('result_model');
$data['query'] =$this->result_model->result_getall();
print_r($data['query']);
die();
$this->load->view('result_view', $data);
}

how codeigniter join two tables to have both Id columns

try this:

 $this->db->select('*,comment.id as comment_id,users.user_id as user_id,users.name as user_name');
$this->db->from('comment');
$this->db->where('user_id', $user_id);
$this->db->join('users', 'users.user_id = comment.id');
$this->db->order_by("comment.id", "asc");
return $this->db->get()->result_array();

it return all user and comment table data

may be this codeigniter query help you out

Codeigniter join two table based on two join conditions

Try:

$this->db->select("Query.*, uf.fullname as firstMD, us.fullname as secondMD");
$this->db->join('Users uf', 'uf.id = Query.attendingMD');
$this->db->join('Users us', 'us.id = Query.secondAttendingMD');
$this->db->where('Query.isCompleted', 1);
$query = $this->db->get('Query');
return $query->result_array();

How to join 2 tables in codeigniter PHP?

Go to folder "Models" That´s where you´ll do all your CRUD operations to the database.
For example, you can create a model file called "guest_profile_model.php"

Now you open that file and paste the code bellow:

       <?php

class guest_profile_model extends CI_Model {

public function select($company_id) {

$this->db->query("select * from `guest_info` INNER JOIN `guest_profile` ON guest_info.g_uid = guest_profile.g_uid WHERE guest_info.company_id = ".$company_id." LIMIT 2");
$result = $this->db->query($query);

return $result->result_object();
}
}

That´s it, now from within your controller you just need to load and call it.
Example:

  $this-> load-> model('guest_profile_model ');
$company_id = 1;
$queryResult = $this -> guest_profile_model ->select(company_id);

print_r($queryResult);

Notice that the query return is going to be an object, that is because of the "result_object" in the model, you can change that to be an array if you want to.



Related Topics



Leave a reply



Submit