Please help me how can I get only response where activities.activity_id = response.activity_id? here is my CI_model
public function get_response(){
$this->db->select('*');
$this->db->from('response');
$id = $this->session->userdata('id');
$this->db->where_in('response.user_id', $id);
$this->db->join('activities', 'response.activity_id = activities.activity_id');
$this->db->join('users', 'users.id = response.user_id');
$result = $this->db->get()->result_array();
return $result;
}
My Activities table
My Response Table
My users table
Try with this:
$id = $this->session->userdata('id');
$this->db->select('a.*, b.*, c.*');
$this->db->join('activities b', 'a.activity_id = b.activity_id');
$this->db->join('users c', 'c.id = a.user_id');
$this->db->where_in('a.user_id', $id);
$result = $this->db->get('response a')->result_array();
return $result;
You need to add aliases to the tables in order to build a more simplified and ordered query, in this case the aliases are a, b and c.
UPDATE: I fix a get method incorrectly writed when copy your code.
Check the query snippet shared here:
https://extendsclass.com/mysql/526c246
I hope to be helpful
Related
I am attempting to create a query to get a single blog post from my blog table and at the same time get the information from that user on my users' table which was ok using Join but now I want to count the total comments of that blog post as total so that would be three tables to query blog, users and comments
But below code display 3 blog entry with the same content, and where to place the COUNT(*) as total for comment table, any suggestion would be great!
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('*, u.ID');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Count the comment id and group the query by blog id.
public function get_entry(){
$id = $this->input->post('ID', true);
$this->db->select('u.*, a.*, count(b.ID) as total');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT')
->group_by('u.ID');
$result = $this->db->get();
if($result->num_rows() > 0){
return $result->result_array();
}else{
return false;
}
}
Try using this, you can change the query according to your requirement:-
$usr_flds = "count(u.ID) as count_rows";
$this->db->select('usr_flds');
$this->db->where('u.ID', $id)
->from('gb_blod as u')
->join('gb_users as a', 'u.user_email = a.email', 'LEFT')
->join('gb_comments as b', 'u.ID = b.journal_id', 'LEFT');
$result = $this->db->get();
return $res->num_rows();
try this.i hope it will work for you.
$result = $this->db->get();
$count = $result->get()->num_rows();
return [$result, $count];
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.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
this would generate NULL output why?
When you are using join query, Please use alias like given below.
$this->db->from('fee_collections AS ac');
$this->db->join('class AS cs','cs.class_id = ac.fee_collection_class_id');
Please try this, This will help you to get output.
Thanks for your edit!
This edit will be visible only to you until it is peer reviewed.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
#Ganesh : Please check my below mentioned query, As per db schema shared by you it's in my local system.
$this->db->select('c.name as cname, s.name as sname,fc.*');
$this->db->from('fee_collections fc');
$this->db->join('class c', 'c.class_id = fc.fee_collection_class_id', 'left');
$this->db->join('student s', 's.student_id = fc.fee_collection_roll_id', 'left');
$this->db->join('fee_particulars fp', 'fp.fee_particular_id = fc.fee_particular_id', 'left');
$this->db->join('admin a', 'a.admin_id = fc.fee_collection_added_by');
$this->db->where('fc.fee_collection_receipt', $receipt_id);
$query = $this->db->get();
return $query->row();
Please change select() line as per your table field name.
Let me know if you have any error.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->where('fee_collection_id',$receipt_id);
$this->db->join('class','class_id = fee_collection_class_id');
$this->db->join('student','student_id = fee_collection_roll_id');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id');
$this->db->join('admin','admin_id = fee_collection_added_by');
$query = $this->db->get();
return $query->row();
}
You are not getting any sql error means your query is right but you are not getting data means your condition is wrong.
Without see the table structure its tough to say exact reason. By seeing your code look like right but you can check same sql query by writing raw sql query. It seems some table not meet joinned condition.
function getReceiptData($receipt_id){
$this->db->select('class.name as cname,student.name as sname,student.father_name as fname,
student.student_parent_email rmail,student.address as raddress,admin.name as aname,fee_particular_name as pname,
fee_particular_discount as discount,fee_particular_amount as pamount,fee_category_id as cat_id, fee_collections.*');
$this->db->from('fee_collections');
$this->db->join('class','class_id = fee_collection_class_id', 'left');
$this->db->join('student','student_id = fee_collection_roll_id', 'left');
$this->db->join('fee_particulars','fee_particular_id = fee_collection_particular_id', 'left');
$this->db->join('admin','admin_id = fee_collection_added_by');
$this->db->where('fee_collection_id',$receipt_id);
$query = $this->db->get();
return $query->row();
}
I am using multiple joins but got stuck in this. I am using join of 3 tables but it fetches values of only 2 tables not 3rd one. Here my model query is:
public function seller_products()
{
$this->db->select('*')->select('wc_seller_products.id')->from('wc_seller_products')
->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id', 'LEFT')
->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id', 'LEFT');
$query = $this->db->get();
return $query;
}
It doesn't fetch values of wc_seller table .... please help
public function seller_products()
{
$this->db->select('wc_seller_products.*,wc_seller.*,wc_seller_info.*');
$this->db->from('wc_seller_products');
$this->db->join('wc_seller', 'wc_seller.id = wc_seller_products.seller_id');
$this->db->join('wc_seller_info', 'wc_seller_info.id = wc_seller_products.seller_id');
$query = $this->db->get();
return $query;
}
I'm trying to get an entire row values from a new SQL query so.
How can I make a function
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
to something like this :
In model page
public function getID_researcher($lastname){
$query = $this->db->get_where('researcher', array('lastname' => $lastname));
return $query->row_array();
}
I need to return one row result base on the lastname which is from table1
To clarify here, 'researcher' is the table where it gets data but what I want is the new SQL for me to get the data.
I tried this one but still error.
public function getID_researcher($lastname){
$sql = = $this->db->query('Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id');
$query = $this->db->get_where($sql, array('lastname' => $lastname));
return $query->row_array();
}
to use this query
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
you can try to use active record db 'join' like this:
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't1.t1_id = t2.t1_id', 'left');
$this->db->join('table3 t3', 't3.id = t2.t3_id', 'left');
$this->db->where('lastname', $lastname);
$query = $this->db->get();
$c = 0;
foreach($query->result() as $q){
$result[$c]['name'] = $q->lastname;
// put move variable here
$c++;
}
return $result;
hope this help