Sanket Deshpande
Sanket Deshpande

Reputation: 107

how to join tables in codeigniter

I have two tables created using phpmyadmin.

Tables - students{s_id,name,address,phone no,branch}

and

events{s_id,event_id,event_name,date,time,venue}.

how to get names of event registered by each student? how to write this query in CodeIgniter?

Upvotes: 0

Views: 71

Answers (2)

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

You can call this with a function and you can return the variable also for further purpose from the model.

If you use function you have to use return $query->result(); at the last line of the function that you write.

But the basic structure is that how i have mentioned below.

$this->db->select('students.*,events.*');
$this->db->from('students');
$this->db->join('events', 'students.sid = events.event_id', 'left'); 
$query = $this->db->get();// This will retrieve all the fields after joining

Functional Method:

function getAllResults()
{
$this->db->select('students.*,events.*');
$this->db->from('students');
$this->db->join('events', 'students.sid = events.event_id', 'left'); 
$query = $this->db->get();// This will retrieve all the fields after joining
return $query->result();
}

Upvotes: 1

Noman
Noman

Reputation: 1487

I just give you a hint so you can solve by yourself:

function yourFunction(){
  $this->db->select('students.*,events.*');
  $this->db->from('students');
  $this->db->join('events', 'students.sid = events.event_id', 'left'); 
  $query = $this->db->get();
  return $query->result();
}

For further knowledge go THERE:

Upvotes: 0

Related Questions