10

I am trying to join two tables and display there data in single table. I looked at the user guide that codeigniter has and I am not sure how this works.

$this->db->join();

What table should be first and what id key should be first. Can someone explain me more in detail about this please use examples if you can. I am trying to join credential table and tblanswers.

I have tried to code a function using this example:

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

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

EDIT: Instead of using join method in CodeIgniter is it possible to use a simple function to retrieve the two table data separately? All I want is to echo the data from database table on to a html table in my website page to be displayed is it possible to write two get functions to retrieve two tables separately?

2
  • You can do that creating two methods on your model and each one does the query you want. Commented Mar 4, 2013 at 11:52
  • While "How to use CodeIgniter's join()" is an on-topic question, I don't actually see where your coding attempt is failing. What are you having trouble with? If you want a full MVC solution, we're going to need more details about database, and your desired output. Commented Oct 24 at 2:43

5 Answers 5

21

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();
?>
Sign up to request clarification or add additional context in comments.

Comments

3

here is how it works:

  1. suppose we have two tables namely student, library.

note: but remember that one of the column should match if you want to use where condition/ here we assume that both tables have std_id column

  1. Write the the select query as follows, in the brackets write what are all the things you want

note:write as shown below don't put quotes to each single one put it on whole at once.

*note: suppose we want name, phone_no. from student table and book_name form library table.*

      $this->db->select('name, phone_number, book_name');
  1. Now write the from query and write one of the table name(No rule)

      $this->db->from('student');
    
  2. Now join this with the another table with join query

      $this->db->join('library', 'students.std_id=library_std_id');
    
  3. Now write the where condition that like you want book name form library table where std id=1(in practical you need to fetch this id from view/database)

      $this->db->where('std_id', 1);
      $q= $this->db->get();
    

That's it it's done now you can print and check the result.

Comments

1
$this->db->join('comments', 'comments.id = blogs.id');

With this line you tell: search me inside comments all comments with id equal blogs.id.

Usually is something like that I think:

$this->db->join('comments', 'comments.blogs_id = blogs.id');

You have to insert into your table a field named blogs_id (int value unisgned) because blogs can have more comments. Isn't important the position of first or second value

4 Comments

so the join will only work if the two table data has the same id ? for example tblanswers has id 12 and credentials have id 12
in your case yes but I don't know for your database if is correct
if the ids have be the same would there be an error duplicate's id ?
yeap a tblanswer and credentials
1

Hi this will work for joining two tables in codeIgnator.

$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
      $this->db->from('chat');
      $this->db->join('post', 'chat.id = post.id');
      $query = $this->db->get();

    if($query->num_rows() != 0)
    {
        return $query->result();
    }
    else
    {
        return false;
    }

You can change to the query as you like do trail and error method to get your appropriate results.

Comments

0

This is my code for joint many tables as possible.

I have 3 tables professeurs,publications and support.

public function toutesdonnées(){

    $this->db->select("*");
      $this->db->from('publication');
      $this->db->join('support', 'publication.idsup = support.idsup');
      $this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
      $query = $this->db->get();

    if($query->num_rows() != 0)
    {
        return $query->result();
    }
    else
    {
        return false;
    }

1 Comment

I never recommend returning false from a model method that ordinarily returns an array payload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.