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.
<<>斯特凡>
//..
$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>
预 收
询问:
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