Unable to get records by join with where clause - php

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 ....

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

Codeigniter query count() total comments joining three tables

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

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.

Combining 2 sql queries in codeigniter

I have two tables. voting_ip and country. I want to retrieve results from country table where open_id_fk (foreign key) of voting_ip table is equal to open_id(Primary Key) of country table. How to write sql query to combine these queries and return the result. I am using the below code in my codeigniter model to retrieve number of occurances of open_id_fk in voting_ip table.
public function mostvotedans()
{
$this->db->select('open_id_fk, COUNT(*) as total');
$this->db->group_by('open_id_fk');
$this->db->order_by('total', 'desc');
$query = $this->db->get('voting_ip', 5);
return $query;
$query = $this->db->query("SELECT * FROM country WHERE open_id_fk=open_id;");
return $query;
}
change it as following.
public function mostvotedans()
{
$this->db->select('c.open_id,COUNT(ip.open_id_fk) as total')->from('voting_ip ip');
$this->db->join('country c','c.open_id=ip.open_id_fk');
$this->db->group_by('ip.open_id_fk');
$this->db->order_by('total', 'desc');
$this->db->limit(5);
$query = $this->db->get();
return $query;
}
Use a join statement :
$query = $this->db->select('v.open_id_fk, COUNT(v.*) AS total, c.country_name')
->join('country AS c', 'c.open_id = v.open_id_fk')
->from('voting_ip AS v')
->group_by('v.open_id_fk')
->order_by('total', 'DESC')
->limit(5);
Should work, I put 'country_name' because I don't know your tables.

Categories