Codeigniter JOIN multiple tables - php

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

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

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

Related

how to get json value on condition from database in codeigniter

public function cmplFirstRdDetails($userMobileNo) {
$this->db->from('records');
$this->db->like('player1', '{"PhoneNumber":"$userMobileNo"}');
$query = $this->db->get();
return $query->result();
}
This code is not working for me.

How to call 2 model function in one function controller in codeigniter

I am new to codeigniter, so please help me to solve my problem.
I want to call my two model function in one function controller. Heres my two model function:
public function getposts($postid){
$this->db->select('*');
$this->db->from('tbl_endangered');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
//$this->db->join('tbl_images');
$this->db->where(['endangeredid'=>$postid]);
$query = $this->db->get();
return $query->result();
}
And here is my function controller:
public function viewAnimals($postid){
if(!$this->session->userdata("id") )
return redirect("AuthCon/login");
$this->load->model('Show');
$posts = $this->Show->getSinglePost($postid);
//$posts = $this->Show->getposts($postid);
$this->load->view('Showimage',['posts'=>$posts]);
}
In that code above I always get the first one that I declared. I want to fetch the getposts and getSinglePost inside the function viewAnimals controller.
Hopeyou can help me. Thanks in advance
I think using same variable name is creating a mess.
you can try assigning to posts array with output in it.
$this->load->model('Show');
$posts['getSinglePostData'] = $this->Show->getSinglePost($postid);
$posts['getPostsData'] = $this->Show->getposts($postid);
$this->load->view('Showimage',$posts);
Do like this in the controller
public function viewAnimals($postid){
if(!$this->session->userdata("id"))
return redirect("AuthCon/login");
$this->load->model('Show');
$data['images'] = $this->Show->getSinglePost($postid);
$data['endangered'] = $this->Show->getposts($postid);
$this->load->view('Showimage', $data);
}
Another way is to use join() and only one model function
public function getSinglePost($postid){
$this->db->select('*');
$this->db->from('tbl_images');
$this->db->join('tbl_endangered', 'tbl_endangered.endangeredid = tbl_images.endangeredid');
$this->db->where('tbl_images.endangeredid', $postid);
return $this->db->get()->result();
}

Join 3 tables with codeigniter and display it

I want to create a join database. in the 'relation' table will show 'disease' which can add many 'symptoms'. How should I do it?
Here is my database:
disease : id_disease, name, details
symptoms : id_symptoms, name, details
relation : id_relation, id_disease, id_symptoms
and here is my model
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
}
Please help me.
You had a typo: instead of relation you had relastion.id_disease.
Code below should work
function get_allrelation() {
$this->db->select('*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relation.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms= relation.id_symptoms','left');
$query = $this->db->get();
return $query->result();
}
The controller below is a example only in the select $this->db->select('relation.*, disease.*, symptoms.*');
Oon the controller you can pass the data to the view the create a foreach loop in the view.
<?php
class Somecontroller extends CI_Controller {
public function index() {
$data['allrelation'] = $this->allrelation();
$this->load->view('example', $data);
}
function get_allrelation() {
$this->db->select('relation.*, disease.*, symptoms.*');
$this->db->from('relation');
$this->db->join('disease','disease.id_disease = relastion.id_disease','left');
$this->db->join('symptoms','symptoms.id_symptoms = relation.id_symptoms','left');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
}
return false
}
}

Using where condition getting blank page in codeigniter mysql

Hi i have used where condition in mysql after using that if i run the website getting the blank page.
Actually i am three department like QA Department,Development,Management.
IN Mysql i have used where condition to get only QA department list but getting blank page.
Here is my code:
Controller:
public function index()
{
$data['records2'] = $this->index_model->get_ourteammembers();
$data['mainpage'] = "our-team";
$this->load->view('templates/template',$data);
}
Model
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department'='QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
Try This
function get_ourteammembers()
{
$this->db->select('T.*');
$this->db->from('ourteam AS T');
$this->db->where('T.department','QA Department');
$q = $this->db->get();
if($q->num_rows()>0)
{
return $q->result();
}
else
{
return false;
}
}
As Per CodeIgniter Document you have to use ',' instead of '='
$this->db->where('T.department'='QA Department');
To
$this->db->where('T.department','QA Department');

fetching data from model to controller to view not working

I'm new in CI and I want to fetch data from DB using codeigniter in MVC way.
I can't seem to find any solution.
model: company.php | dbname: company
public function getAllcompany(){
$query = $this->db->get('company');
if($query->num_rows() > 0)
{
return $query->result();
}
return array();
}
view: branch.php
<?php
echo "<pre>", print_r($records), "</pre>";
?>
controller: home.php
public function getCompany(){
$this->load->model('company','', TRUE);
$data['records'] = $this->company->getAllcompany();
$this->load->view('branch', $data);
}
You will have to change your model to :
public function getAllcompany(){
$query = $this->db->get('company');
return $query->result();
}
OR
public function getAllcompany(){
$query = $this->db->get('company');
return $query->result_array();
}

Categories