1

I'm having a little trouble in retrieving data in multiple tables using codeigniter.

This is the code i'm using to retrieve data in my model which is working well.

function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;  
}

function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}

Now i tried using a simplified code but fails to display the data. here is the code in my model

function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}

In my controller, i used this code to retrieving data in my model

function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}

and for the display i used this code

foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name']; 
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}

I cant display anything at all. Please help

3
  • You have any SQL error? Try enable codeigniter profiler to see what query you actual running. Commented Oct 10, 2015 at 10:28
  • On view echo $results['company_name']; or <?php echo $results['company_name'];?> Commented Oct 10, 2015 at 10:41
  • @wolfgang1983 yes i used echo, still no output.. forgot to put echo on my question. my bad Commented Oct 10, 2015 at 11:20

2 Answers 2

1

Try the below code,

I believe you'll want something like this:

function retrieve_all_data($alumni_id)
{
  $this->db->select("e.*,edu.*");
  $this->db->from("experience e");
  $this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
  $this->db->where('e.alumni_id',$alumni_id);
  $this->db->group_by('e.exp_id');
  $query = $this->db->get();
  return $query->result_array();
}
Sign up to request clarification or add additional context in comments.

24 Comments

it only displayed 1 result instead of 3 when i added $this->db->group_by('e.alumni_id');
@Chris how do you actually want it??
imgur.com/a/8aDta i want to display all the data in EXPERIENCE table WHERE alumni_id = $alumni_id. in my experience table, there are 3 entries in alumni_id=14 in the 1st image, it displayed 3 data, (using my old function function retrieve_experience($alumni_id) { $this->db->select('*'); $this->db->from('experience'); $this->db->where('alumni_id',$alumni_id); $query = $this->db->get(); return $query; } 2nd image is the code im using and the output is the 3rd image
@Chris try my code above..I have used left join...i can't see that in your code.
yes i added 'left' on the join. still same result. sorry forgot to update the screenshot image
|
1

Hope below mentioned function should return data that you expected.

function retrieve_all_data($alumni_id)
{
  $this->db->select('*');
  $this->db->from('experience ex');
  $this->db->join('education ed','ed.alumni_id=ex.alumni_id');
  $this->db->where('ex.alumni_id',$alumni_id);
  $query=$this->db->get();
  return $query->result_array();
}

3 Comments

still no data displayed.
Please make a try catch block to see the error. function retrieve_all_data($alumni_id) { $this->db->select('*'); $this->db->from('experience ex'); $this->db->join('education ed','ed.alumni_id=ex.alumni_id'); $this->db->where('ex.alumni_id',$alumni_id); try{ $query=$this->db->get(); return $query->result_array(); }catch(Exception $e){ echo $e->getMessage(); die(); } }
my bad. it retrieved and displayed data. but the problem now is that the data displayed was looped 3times and i dont know why.

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.