Join tables and display city.name - php

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.

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 how to avoid duplicates showing one to many relationship

I have been trying to show rows without duplicates but the query isn't working properly. I think the problem is one to many relationship, because one 'intervaloHorario' has many 'citas'. So, for example, i want to show only: 'From 8:00 to 15:00 (this is an intervaloHorario)' to date (cita) '27/08/1988'. What should i do?
Controller
$this->Fechacita_Model->delete_duplicaterow();
Model
public function delete_duplicaterow() {
$this->db->select('
c.intervaloHorario','ci.cita'
);
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Model(EDIT)
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario', 'left');
$this->db->group_by('c.idIntervaloHorario', 'ci.cita');
$query = $this->db->get();
$this->db->last_query();
return $query->num_rows();
Database
Current database
Screenshot
Current list (unordered list but duplicates persists)
You can use $this->db->distinct() and add selecting primary key to remove duplicate:
public function delete_duplicaterow() {
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Use the keyword DISTINCT in your query
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html

how to join multiple joins in codeigniter

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

How to make Join Query model in CodeIgniter

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

Categories