Show join table results even on condition is null - php

I have five table name books,categories,publications,author,reviews.
in my app user can review the book after login. so reviews table stay empty until user post a review.
I am trying to run the flowing query in codeigniter model to get book details by category_id and it's working perfect when their review is exist otherwise it's return empty array. this is happening because of this $this>db>join('reviews','reviews.book_id = books.book_id'); condition return false
how can i show result even reviews table on condition is not match?
public function get_book_details($cat_id) {
$this->db->select('*');
$this->db->from('books');
$this->db->join('categories', 'categories.id = books.category_id');
$this->db->join('publications', 'publications.id = books.publication_id');
$this->db->join('author', 'author.id = books.author_id');
$this->db->join('reviews', 'reviews.book_id = books.book_id');
$this->db->where('categories.id', $cat_id);
$query = $this->db->get();
return $query->result();
}

Use LEFT JOIN to get the result if there are no matching associations are found
$this->db->join('reviews', 'reviews.book_id = books.book_id','LEFT');

Related

Show data record using id in codeigniter

i got this error message
Column 'id_siswa' in where clause is ambiguous
SELECT * FROM `siswa` `a`
LEFT JOIN `pembayaran_spp` `b` ON `b`.`id_siswa`=`a`.`id_siswa`
WHERE `id_siswa` = '7%E2%80%8B'
I have 2 table.
1.table 'siswa'
structure(id_siswa,nama_siswa,id_tahun_masuk)
2.table 'pembayaran_spp'->
structure(id_pembayaran,id_siswa,jml_pembayaran,id_tahun,date)
I want to show data 'pembayaran_spp' by id_siswa.
so when i click detail on 'siswa', data 'pembayaran' is showed by id_siswa.
My Controller
function detailtagihan($id_siswa)
{
$data['siswa'] = $this->M_keuangan->tagihansiswa($id_siswa);
$this->load->view('template/header');
$this->load->view('template/sidebar');
$this->load->view('keuangan/v_detailtagihan',$data);
$this->load->view('template/footer');
}
My Model
function tagihansiswa($id_siswa)
{
//$data = array('id_siswa' => $id_siswa );
$this->db->select('*');
$this->db->from('siswa a');
$this->db->join('pembayaran_spp b','b.id_siswa=a.id_siswa', 'left');
$this->db->where('id_siswa',$id_siswa);
$query = $this->db->get();
if($query->num_rows()>0)
return $query->result();
}
You should mention of which table you want to use the column id_siswa in your where clause. As both of the tables are having a column with same name, you are getting this error.
If you want to use siswa then in your where condition write a.id_siswa
And if you want to use pembayaran_spp then in your where condition b.id_siswa.

join two tables in codeigniter and refere to two fields in the same table

My application works like this: α user creates a task and assigns it
to another user. I have two tables
(user table)
user_name
AND
(task table)
task_subject
creator
assigned_to
my question how to make join statement by codeigniter to display
the task, its creator name and assigned to whom.
Please check following query for join in codeigniter
$user = 'user';
$task = ' task,';
$this->db->select($user.'.*,'.$task.'.*');
$this->db->from($task);
$this->db->join($user,
$task.'.user_id = '.$user.'.user_id'
);
$this->db->join($user,$user.'.user_id = $task.'created_by','left');
$query = $this->db->get();
if ($query->num_rows() > 0){
$result = $query->result();
return $result;
}
return FALSE;
Instead of using * you can use your own custom fields that you want to show and in join use the common id in both tables like I use user_id

Joining two tables in codeginiter

I have 2 tables that i want to join & show the name of user's role. here's the situation
My 2 tables are users_mlh & user_roles_mlh
on the role column of users_mlh table i'm storing the ID of user role, user_roles_mlh contains the name & id of user role. what i want to do is show the name of the user role in my view.
my tables as follows.
i have tried this in my model
$this->db->select('*');
$this->db->from('user_roles_mlh');
$this->db->join('users_mlh', 'users_mlh.role = user_roles_mlh.id');
$this->db->where('users_mlh.role = user_roles_mlh.id');
$query = $this->db->get();
return $query->result_array();
but from above i get something like this
at the moment it lists all user level not the role of each individual user
Case 1 : If you Directly want to Access all data without using where condition
$this->db->select( "*" );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
Case 2: With Where Condition and specific column data
$this->db->select( 'user_roles_mlh.role_type,users_mlh.name' );
$this->db->from( 'users_mlh' );
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$this->db->where('users_mlh.id =',1);
And finally get the results by
$query = $this->db->get();
return $query->result();
No need to write separate query for getting role name. Join Roles table when you fetching users data..
$this->db->select('users_mlh.*,user_roles_mlh.role_type');
$this->db->from('users_mlh');
$this->db->join('user_roles_mlh', 'user_roles_mlh.id = users_mlh.role');
$query = $this->db->get();
return $query->result_array();
Try this.
If I use your tables, you should have the following request:
SELECT users_mlh.*, JOIN user_roles_mlh.role_type
FROM users_mlh -- list all users
LEFT JOIN user_roles_mlh -- and joind by roles
ON users_mlh.role = user_roles_mlh.id -- condition for joining
With this request, you will have all your users, with the role text associated.
So, if this request works, use the same logic on your ORM

Codeigniter 3 joining 2/3 tables

I'm using Codeigniter and I need to join 2/3 tables. I can't quite work it out.
I have a users table with general user info.
user_id
first_name
last_name
I have a course stats page with info like due date etc.
id
course_id
user_id
due
completed_on
A user will pick a course, and then be presented with a list of users of their organization. Ready to be assigned. The user may already be assigned to the course and have stats in the stats table while other users may not have stats.
So I need to display a list of users with any stats to the chosen course, if the stats do of course exists.
SO, I'm sending to the model an org_id (to get a list of users of that org) and the chosen course_id (to retrieve any stats from he stats table if they exist)
Here's my method in the model
// get users for an org along with stats
public function get_course_user_data($org_id,$course_id) {
$user_array = array('users.org_id =' => $org_id);
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
$this->db->order_by('users.first_name', 'asc');
$this->db->where($user_array);
$this->db->join('training_stats','training_stats.user_id = users.id', 'left');
$this->db->join('training_courses','training_courses.course_id = training_stats.course_id', 'left');
$user_object = $this->db->get('users');
return $user_object->result();
}
At the moment, if there are stats in the stats table then it displays data, if there are no stats in the stats table, I get nothing.
But I need a full list of users and only the stats if they exist.
Can I even do this with a join? I would have though so?
Try this query:
// get users for an org along with stats
public function get_course_user_data($org_id,$course_id) {
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
$this->db->order_by('users.first_name', 'asc');
$this->db->join('training_stats','training_stats.user_id = users.id', 'left');
$this->db->join('training_courses','training_courses.course_id = training_stats.course_id', 'left');
$this->db->where("( training_courses.course_id = $course_id AND training_stats.course_id = $course_id)" );
$this->db->or_where( "( users.org_id = $org_id )" );
$user_object = $this->db->get('users');
return $user_object->result();
}
I think you forgot something...
$this->db->from('table_name');
after this....
$this->db->select('users.id,users.first_name,users.last_name,training_stats.due,training_stats.completed_on');
I hope this help

codeigniter join 2 table data

hi everyone i am new to codeigniter and currently working on a small project in the project i am trying to join two tables and display there data in single table. i looked at the user guide that codeigniter has an i am not sure how this work
$this->db->join();
what table should be first and what id key should be firs. Can someone explain me more in detail about this please use examples if u can. I am trying to join credential table and tblanswers. Tnx for answering.
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 ?
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();
?>
here is how it works:
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
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');
Now write the from query and write one of the table name(No rule)
$this->db->from('student');
Now join this with the another table with join query
$this->db->join('library', 'students.std_id=library_std_id');
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.
$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
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.
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;
}

Categories