How to select join multiple table without conditional in Codeigniter? - php

I want to select multiple table using join in codeigniter but I don't want to make any conditional as below code
public function get_invoice(){
$this->db->select('isp.*,dp.*,ip.*');
$this->db->from('isp');
$this->db->join('dp','dp.status = isp.status');
$this->db->join('isp','isp.status = ip.status');
$this->db->limit(5000);
$this->query = $this->db->get();
if($this->query->num_rows()>0){
return $this->query->result();
}
}
however I want to get all data in that table without conditional but in join statement have to used conditional.
So how can I select data from multiple table without conditional in codeigniter?
Thanks for help

Yes you can join query without condition. INNER LEFT RIGHT JOINS need condition.So you can do Cross JOIN.LIKE this
public function get_invoice(){
$this->db->select('isp.*,dp.*,ip.*');
$this->db->from('isp,dp,ip');
$this->db->limit(5000);
$this->query = $this->db->get();
if($this->query->num_rows()>0)
{
return $this->query->result();
}
}
But at Cross JOIN if your isp has 100,dp has 20, and ip has 50 record It will produce (100*20*50) record.I hope you know what CROSS,LEFT,INNER,RIGHT JOIN do.

Related

Codeigniter Right Join with Where condition not giving the result

This is my code in model
function load_roster_by_date()
{
$date = $this->input->post('date');
$this->db->select('*');
$this->db->from('duty_assign');
$this->db->where('date',$date);
$this->db->join('opd_employees','(opd_employees.badgenumber = duty_assign.emp_id)','right');
$query = $this->db->get();
return $query->result_array();
}
The code perfectly works without adding $this->db->where('date',$date);. (Which means the output is the expected right joined table without a where condition). But when I add the where condition, the result is similar to the regular join, not as the right join. No errors in the console. Any help to solve this, please?
All I want is whether a duty added or not all the employee names
should be displayed in the table
$this->db->where('date',$date);
can you try to:
$this->db->where('opd_employees.date',$date);
or
$this->db->where('duty_assign.date',$date);
cause this db already select join
I think this will help you understand the difference between Joins.
There must be no data in your database.
$this->db->get_compiled_select(); => It will help you see the query made be CI which you can execute directly on your server to see the results.
You can see this answer What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN? to understand "Joins".
Please add where condition below the join.
function load_roster_by_date()
{
$date = $this->input->post('date');
$this->db->select('*');
$this->db->from('duty_assign');
$this->db->join('opd_employees','(opd_employees.badgenumber = duty_assign.emp_id)','right');
$this->db->where('date',$date);
$query = $this->db->get();
return $query->result_array();
}

Data is not fetched from database

I am trying to get data from others table but when I join serviceplan table the result of query is empty but when I remove serviceplan table its fetched data perfectly.
Here is the code of my Model(without serviceplan table-working)
public function send_mail() {
// $user_id=$this->session->userdata('user_id');
$query=$this->db->select('*, employee.name_emp as emp_name, customer.name as cust_name, servicetype.name_set as s_name',
'serviceplan.price as p_rice')->from('appointment')
// ->where('id_app',$appointment_id)
->join('customer', 'customer.id= appointment.name_app')
->join('servicetype', 'servicetype.id_set= appointment.sertype')
->join('employee', 'employee.id_emp= appointment.emp')
// ->join('serviceplan', 'serviceplan.id_sep= appointment.price_app')
->get();
echo $this->db->last_query();
exit();
return $query->result();
}
Normal joins or Inner Join only gets the rows common in both the tables. So, in your case, there are not matching conditions in your serviceplan table.
Verify it. Or look upon your requirements and try using leftjoin instead of join.
Also look: Difference between joins
You may try this simple join table query using codeigniter as below:
$email='myEmail#test.com';
$this->db->select('*');
$this->db->from('table1');
$this->db->where('table1.email',$email);
$this->db->join('table2', 'table1.email = table2.email');
$query = $this->db->get();
$assignedData=$query->result_array();

How can I display fields from two different tables using active record of left join in codeigniter?

function get_all_notification() { //get all notification based on the 'notification' table left join by 'membership' table
$this->db->select()->from('notification')->join('membership','membership.membership_id = notification.notif_id','left'); $notif = $this->db->get();
return $notif->result();
}
I cannot display the fields from membership table.
Try this
function get_all_notification() { //get all notification based on the 'notification' table left join by 'membership' table
$this->db->select('*')->from('notification')->join('membership','membership.membership_id = notification.notif_id','left');
$notif = $this->db->get();
return $notif->result();
}
function get_all_notification()
{
$this->db->select('n.*,m.*');
$this->db->from('notification as n')
$this->db->join('membership as m','m.membership_id = n.notif_id','left');
$notif = $this->db->get();
return $notif->result();
}
This is used when you want a clean code that's from top to bottom. But using your code is fine to where as the arrows are continuous. You just forgot to identify which fields to select. Also using aliases are good in selecting specific fields.
as you can see, I used n.* and m.*, this is just a wildcard to select all from the n table and m. you can you n.your_field and m.your_field when specifying what you want.
Goodluck and have fun coding :D

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

Running 2 queries in model (codeigniter)?

For example, if I run one query in model:
public function list_users() {
$q = "SELECT user_id, username FROM users";
return $q->result_array();
}
And now, to lists posts from that user, I need to refer to it's id within this function:
public function list_posts() {
$q = "SELECT post_id, post_title, post_content FROM posts
WHERE user_id = what??";
return $q->result_array();
}
OK both of these functions are in Model. Now, How to use RESULT from list_users() in list_posts(). Please have in mind that I need to pass ARRAY of IDs and, to use it only for particular id from list_users() which also returns ARRAY
I know I can use joined query, but that's not the point at all, as I have lots of queries that I need to split
why arent you using a JOIN statement, and making two queries into one?
This will reduce db load, decrease query times, and also reduce clutter in your models.
SELECT p.post_id, p.post_title, p.post_content, u.user_id, u.username FROM posts p LEFT JOIN users u ON u.user_id = p.user_id
you can also do this using active records. Which will avoid having to use full blown queries, and more of a CI methodology to SQL.
http://codeigniter.com/user_guide/database/active_record.html
$this->db->select('p.post_id, p.post_title, p.post_content, u.user_id, u.username');
$this->db->from('posts p');
$this->db->join('users u', 'u.user_id = p.user_id');
$q = $this->db->get();
$q->result();
Edit:
You can return the value as an object.. IE: $this->user_id then reference it in the posts function. Ideally you should call the first function in your Controller, return $user_id and then reference that in your next function.. This is definitely not best case though, you should use JOINs as they are less taxing on the db.
//controller
function test(){
$users = $this->exampleModel->list_users();
//manipulate user data if needed
$posts = $this->exampleModel->list_posts($users);
}

Categories