How to make Join Query model in CodeIgniter - php

How to write join query in codeigniter... I want only model like this Select query-
public function getData($col, $table, $where = array())
{
$this->db->select($col);
$this->db->from($table);
$this->db->where($where);
$query = $this->db->get();
$result = $query->result();
return $result;
}
Plz help

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Please go through the user guide before posting it on stackoverflow

join query is there..
try this
$this->db->join('second_table', 'second_table.id = first_table.id');

try like this
$this->db->select('*');
$this->db->from('first_table');
$this->db->join('second_table', 'second_table.col_name = first_table.col_name');
$query=$this->db->get();
if($query->num_rows()>0){
return $query->result_array();
}

for joining table you may use join() methods.
$this->db->from(table1)
$this->db->join('table2','table1.id=table2.table1_id','join options');
on condition means in which condition you want to join tables .
Join options is optional.
Join Options are: left, right, outer, inner, left outer, and right outer

Related

Inner join query in codeigniter

code:
public function draft_post($idd)
{
$this->db->select('*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id','INNER');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
In this codes, I have two table i.e. registration and draft_registration. Now, What am I doing here I want to run inner join in Codeigniter. Now, What happening when I hit this query on phpmyadmin it shows wrong data i.e. I have two rows in draft_registration and one row in registration table but it always shows two table which is wrong and my query looks like when I was print as mention below:
SELECT *
FROM `registration`
INNER JOIN `draft_registration` ON `registration`.`user_id`= `draft_registration`.`user_id`
WHERE `registration`.`user_id` = '20181121064044'
So, How can I resolve this issue? Please help me.
Thank You
$this->db->select('*'); //This code get all rows from both table.If you want a particular row you mention the column name.
For example:
$this->db->select('registration.name,draft_registration.city,draft_registration.state');
Specify column that you want to select. Or if you want select all column of your table, you can use :
SELECT registration.* with backticks `` on column name
Use the Below Code
public function draft_post($idd)
{
$this->db->select('registration.*,draft_registration.*');
$this->db->from('registration');
$this->db->join('draft_registration', 'registration.user_id= draft_registration.user_id');
$this->db->where('registration.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}
Or you can use with objects
public function draft_post($idd)
{
$this->db->select('a.*,b.*');
$this->db->from('registration a');
$this->db->join('draft_registration b', 'a.user_id= b.user_id');
$this->db->where('a.user_id', $idd);
$query = $this->db->get();
$result = $query->result_array();
return $result;
}

Join tables and display city.name

Having issues joining two tables and displaying the other value, tables are setup and models are loaded and starts with a capital letter.
What i want is to match jobs.city_id and cities.id and display cities.city_name
Jobs_model.php
public function get_city(){
$this->db->select('*');
$this->db->from('cities');
$this->db->join('jobs', 'cities.city_name = jobs.city_id');
$this->db->where('jobs.city_id = cities.id');
$query = $this->db->get();
}
Controller.php
$data['city_name'] = $this->jobs_model->get_city();
View.php
<?php echo $city_name['city_name'];?>
SQL
CITIES
id
city_name
JOBS
id
city_id
update. sql and typo
You also have not returned any thing in model function
If you need a specific type of JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.
This function example below only will return one row at a time you may need to use result_array(); read this link below how to generate results https://www.codeigniter.com/user_guide/database/results.html
public function get_city(){
$this->db->select('*');
$this->db->from('cities');
$this->db->join('jobs', 'jobs.city_id = cities.id', 'LEFT');
//$this->db->where('jobs.city_id = cities.id');
$query = $this->db->get();
// Note only returns one row
return $query->row_array();
}
And On Controller
$info = $this->jobs_model->get_city();
$data['city_name'] = $info['city_name'];
Multiple Results
public function get_city(){
$this->db->select('*');
$this->db->from('cities');
$this->db->join('jobs', 'jobs.city_id = cities.id', 'LEFT');
$query = $this->db->get();
return $query->result_array();
}
And on controller
$data['cities'] = $this->jobs_model->get_city();
View
<?php foreach ($cities as $city) {?>
<?php echo $city['city_name'];?>
<?php }?>
I am giving you a simple SQL for reference -
select cities.name from cities, jobs where jobs.city_id = cities.id
Change your Model to
public function get_city(){
$this->db->select('*');
$this->db->from('cities');
$this->db->join('jobs', 'cities.city_id= jobs.city_id');
$this->db->where('jobs.city_id = cities.id');
$query = $this->db->get();
return $query->result_array()
}
You are comparing wrong field in join query
public function get_city(){
$this->db->select('*');
$this->db->from('cities');
$this->db->join('jobs', 'cities.id = jobs.city_id'); //<---id instead of city_name
//$this->db->where('jobs.city_id = cities.id'); // remove where
$query = $this->db->get();
}
Use this
$this->db->join('jobs', 'cities.city_id = jobs.city_id');
instead of this
$this->db->join('jobs', 'cities.city_name = jobs.city_id');
Should be like this:
join('jobs', 'cities.id = jobs.city_id');
I think that's the issue.

how to write simple query into codeigniter query using join right

how to write simple query into codeigniter query using join righ?????
$query = $this->db->query("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,
staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id
FROM staff_role_permissions
RIGHT JOIN staff_permissions_list ON staff_role_permissions.permission_id=staff_permissions_list.id
AND staff_role_permissions.role_id=$id WHERE staff_permissions_list.perm_type=0
ORDER BY staff_permissions_list.id ASC
");
if ($query->num_rows() > 0) {
return $query->result_array();
}
$this->db->select('book_id, book_name, author_name, category_name');
$this->db->from('books');
$this->db->join('category', 'category.category_id = books.category_id', 'right');
$query = $this->db->get();
you can get data using this method of right join
How about that ?
$query = $this->db
->select("Select staff_permissions_list.perm_type,staff_permissions_list.permission_key,staff_permissions_list.permission_label,staff_permissions_list.id, staff_role_permissions.permission_id as p_id,staff_role_permissions.role_id")
->from("staff_role_permissions AS srp")
->join("staff_permissions_list AS spl","srp.permission_id = spl.id","right")
->where("spl.perm_type","0")
->where("srp.role_id",$id)
->order_by("spl.id","ASC")
->get();
i put the role_id to the where section - maybe you need to put it back (not sure what you want to achieve here)

Outer join not working in my app

Outer join in codeigniter not working
$this->db->select('*');
$this->db->from('users');
$this->db->join('userdetails','users.user_id=userdetails.user_id','outer');
$query = $this->db->get();
if($query->result())
return $query->result();
else
return false;
Please help
When you write an outer join in MySQL, the keyword LEFT or RIGHT is required, while OUTER is optional. So use:
$this->db->join('userdetails','users.user_id=userdetails.user_id','left');

How to INNER JOIN 3 tables using CodeIgniter

Can someone Tell me how to join 3 table with php?
Example
SELECT FROM table1, table2,table on INNERJOIN -------------------
let I have 3 table.(question table ,answer table and category table)
Here is example form my webpage.
Time remaining 30 minutes(I will get "30 minutes" form Category table)
1. Question (from question table)
2. answer (from answer table)
I don't know how to join 3 table.
it should be like that,
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
as per CodeIgniters active record framework
I believe that using CodeIgniters active record framework that you would just use two join statements one after the other.
eg:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table1', 'table1.id = table2.id');
$this->db->join('table1', 'table1.id = table3.id');
$query = $this->db->get();
Give that a try and see how it goes.
I created a function to get an array with the values ​​for the fields and to join. This goes in the model:
public function GetDataWhereExtenseJoin($table,$fields,$data) {
//pega os campos passados para o select
foreach($fields as $coll => $value){
$this->db->select($value);
}
//pega a tabela
$this->db->from($table);
//pega os campos do join
foreach($data as $coll => $value){
$this->db->join($coll, $value);
}
//obtem os valores
$query = $this->db->get();
//retorna o resultado
return $query->result();
}
This goes in the controller:
$data_field = array(
'NameProduct' => 'product.IdProduct',
'IdProduct' => 'product.NameProduct',
'NameCategory' => 'category.NameCategory',
'IdCategory' => 'category.IdCategory'
);
$data_join = array
( 'product' => 'product_category.IdProduct = product.IdProduct',
'category' => 'product_category.IdCategory = category.IdCategory',
'product' => 'product_category.IdProduct = product.IdProduct'
);
$product_category = $this->mmain->GetDataWhereExtenseJoin('product_category', $data_field, $data_join);
result:
echo '<pre>';
print_r($product_category);
die;
I think in CodeIgniter the best to use ActiveRecord as wrote above.
One more thing: you can use method chaining in AR:
$this->db->select('*')->from('table1')->join('table2','table1.id=table2.id')->...
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id','JOIN Type');
$this->db->join('table3', 'table1.id = table3.id');
$query = $this->db->get();
this will give you result from table1,table2,table3 and you can use any type of join in the third variable of $this->db->join() function such as inner,left, right etc.
For executing pure SQL statements (I Don't Know About the FRAMEWORK- CodeIGNITER!!!)
you can use SUB QUERY!
The Syntax Would be as follows
SELECT t1.id
FROM example t1 INNER JOIN
(select id from (example2 t1 join example3 t2 on t1.id = t2.id)) as t2 ON t1.id = t2.id;
Hope you Get My Point!
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.id = table2.id', 'inner');
$this->db->join('table3', 'table1.id = table3.id', 'inner');
$this->db->where("table1", $id );
$query = $this->db->get();
Where you can specify which id should be viewed or select in specific table. You can also select which join portion either left, right, outer, inner, left outer, and right outer on the third parameter of join method.
you can modiv your coding like this
$this->db->select('a.nik,b.nama,a.inv,c.cekin,c.cekout,a.tunai,a.nontunai,a.id');
$this->db->select('DATEDIFF (c.cekout, c.cekin) as lama');
$this->db->select('(DATEDIFF (c.cekout, c.cekin)*c.total) as tagihan');
$this->db->from('bayar as a');
$this->db->join('pelanggan as b', 'a.nik = b.nik');
$this->db->join('pesankamar_h as c', 'a.inv = c.id');
$this->db->where('a.user_id',$id);
$query = $this->db->get();
return $query->result();
i hope can be resolve your SQL
function fetch_comments($ticket_id){
$this->db->select('tbl_tickets_replies.comments,
tbl_users.username,tbl_roles.role_name');
$this->db->where('tbl_tickets_replies.ticket_id',$ticket_id);
$this->db->join('tbl_users','tbl_users.id = tbl_tickets_replies.user_id');
$this->db->join('tbl_roles','tbl_roles.role_id=tbl_tickets_replies.role_id');
return $this->db->get('tbl_tickets_replies');
}

Categories