I’am trying to join two tables. Lets say t1 and t2. t1 has fk t2_id. but when I run the code nothing is shown in my view.
Controller:
//..
$data['city']= $this->state_model->name();
$this->load->view('viewt', $data);
Model:
function name(){
$this->db->select('*');
$this->db->from('state');
$this->db->join('city', 'city.state_id = state.id');
$sql = $this->db->get();
if ($sql->num_rows () >0) {
foreach($sql->result() as $row) {
$this->db->where('state_id','state.id');
$r = $this->db->get('city');
}
return $r->result();
}
else {
return null;
}
View:
<?php foreach($city as $row):?>
<?php echo $row->cityname; ?></br></br></br>
<?php endforeach;?></br></br>
Thanks in advance
The Queries:
SELECT `id`, `statename` FROM (`state`) ORDER BY `id` ASC SELECT * FROM (`state`) JOIN `city` ON `city`.`state_id` = `state`.`id` SELECT * FROM (`city`) WHERE `state_id` = 'state.id' SELECT * FROM (`city`) WHERE `state_id` = 'state.id' SELECT * FROM (`city`) WHERE `state_id` = 'state.id'
Your $r variable will only have the last iteration thus the function will only return the last result set, so try this: