Codeigniter query count() total comments joining three tables - php

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

Related

Joining Query with codeigniter Model

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

How to join multiple tables with aliases

I have 3 tables in database:
teams
id
name
matches
id (int)
team_home_id
team_away_id
goals
id
match_id
team_id
time
I need display names of teams in view where I get goals in controller.
I know that I should do join tables.
I have this code:
public function get_goals() {
$this->db->select('goals.*');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$q = $this->db->get();
return $q->result();
}
and I don't know what next.
I need in view display names by:
$goals->team_home_name and $goals->team_away_name
To get $goals->team_home_name and $goals->team_away_name result, use aliases like this :
public function get_goals() {
$this->db->select('goals.*, home_team.name team_home_name, away_team.name team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams home_team', 'home_team.id = matches.team_home_id');
$this->db->join('teams away_team', 'away_team.id = matches.team_away_id');
$q = $this->db->get();
return $q->result();
}
You can try with this:-
public function get_goals() {
$this->db->select('g.* , t.name as team_home_name , t.name as team_away_name);
$this->db->from('goals as g');
$this->db->join('matches as m', 'm.id = g.match_id');
$this->db->join('teams as t', 't.id = g.team_id');
$q = $this->db->get();
return $q->result();
}
U can use an alias by
guss the team_away_name and team_home_name from teams table
public function get_goals() {
$this->db->select('goals.*, team.team_home_name, team.team_away_name');
$this->db->from('goals');
$this->db->join('matches', 'matches.id = goals.match_id');
$this->db->join('teams as team', 'team.id = goals.team_id');
$q = $this->db->get();
return $q->result();
}

Unable to get records by join with where clause

I have 2 tables. first users second post. without where clause I got all user post but I want to display only logged in user data. here is table structure for my tables. both tables has common value by user.id = post.user_id
First Table User Second Table Post
ID id
NAME user_id
Username category_id
Password user_id
title
body
My Query to get all records but doesn't logged in user records
public function get_posts(){
$this->db->order_by('posts.id', 'DESC');
$this->db->join('categories', 'categories.id = posts.category_id');
$query = $this->db->get('posts');
return $query->result_array();
}
Hope this will help you :
Here $user_id is id of logged in user should be passed to the model from controller
public function get_posts($user_id)
{
$this->db->select('*');
$this->db->from('posts');
$this->db->order_by('posts.id', 'DESC');
$this->db->join('users', 'users.id= posts.user_id');
$this->db->where('users.id',$user_id);
$query = $this->db->get();
return $query->result_array();
}
try this,
public function get_posts()
{
$this->db->select('*');
$this->db->from('posts');
$this->db->join('users', 'users.id = posts.user_id');
$this->db->join('categories', 'categories.id = posts.category_id');
$this->db->order_by('posts.id', 'DESC');
$query = $this->db->get();
return $query->result_array();
}
you can use it like this , just replace $user_id variable with your dynamic post user id value
$this->db->select('a.*');
$this->db->from('user a');
$this->db->join('post b', 'b.user_id = a.id', 'left');
$this->db->where('a.id', $user_id);
$data_array = $this->db->get()->result_array();
return $data_array ;
Hope this will help you ....

Multiple Join query in codeigniter?

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

How to write this query in codeigniter active records

How to write this query in codeigniter active records
select
a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,
c.sub_child_cat_id,c.sub_child_cat_name
FROM parent_categories a,child_categories b,sub_child_categories c
WHERE a.parent_cat_id=b.parent_cat_id AND b.child_cat_id=c.child_cat_id
Tried this but it Shows 0 result
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where('a.parent_cat_id','b.parent_cat_id');
$this->db->where('b.child_cat_id','c.child_cat_id');
$result = $this->db->get()->result_array();
when i echo the above ci query i get
SELECT `a`.`parent_cat_id`, `a`.`parent_cat_name`, `b`.`child_cat_id`, `b`.`child_cat_name`, `c`.`sub_child_cat_id`, `c`.`sub_child_cat_name`
FROM `parent_categories` `a`, `child_categories` `b`, `sub_child_categories` `c`
WHERE `a`.`parent_cat_id` = 'b.parent_cat_id'
AND `b`.`child_cat_id` = 'c.child_cat_id'
Try changing $this->db->where in your query as below-
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where("a.parent_cat_id = b.parent_cat_id");
$this->db->where("b.child_cat_id = c.child_cat_id");
$result = $this->db->get()->result_array();
You have to use Join Query For That Here is Code Snippet
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a');
$this->db->join('child_categories b', 'b.parent_cat_id = a.parent_cat_id', 'left');
$this->db->join('sub_child_categories c', 'c.child_cat_id = b.child_cat_id', 'left');
$query = $this->db->get();
$res = $query->result();
I didn't have your table structure and data to check. But, try this. It will work.
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->join('a.parent_cat_id','b.parent_cat_id');
$this->db->join('b.child_cat_id','c.child_cat_id');
$this->db->get();

Categories