这是我的Codeigniter应用程序中的代码,我试图通过它的id获取location
。
问题是我在检索数据时无法获得一致的结果。
模型
function get_location($id)
{
$this->db->select('locations.id, locations.name_ka, locations.text_ka, locations.keyword_ka, locations.region, locations.latitude, locations.longitude, locations.notes_ka, locations.description_ka, locations.recomendation, locations.img, locations.nature, locations.culture, locations.resort, regions.region_type, regions.region_name_ka, resort.resort_name_ka, nature.nature_name_ka, culture.culture_name_ka, gallery.id, gallery.post_id, gallery.img_link, gallery.gallery_name_ka')
->from('locations','id=$id')
->join('regions', 'regions.id = locations.region','left')
->join('culture', 'culture.id = locations.culture','left')
->join('resort', 'resort.id = locations.resort','left')
->join('nature', 'nature.id = locations.nature','left')
->join('gallery', 'gallery.post_id = locations.id','left')
->where('locations.id='.$id);
$queryo = $this->db->get();
return $queryo->row_array();
}
控制器
function details($id){
$this->load->view('templates/head');
$this->load->view('templates/header');
$details = array(
'location' => $this->Location_model->get_location($id)
);
$this->load->view('one_location', $details);
$this->load->view('templates/end');
}
并且存在问题:它仅在第一行获得'id'。对于其他记录获取一切,但不是id。说'id' = NULL;
输出示例
1st row: array(22) { ["id"]=>string(1) "1" ["name_ka"]=>string(28) "მწვანე ტბა"...
2nd row: array(22) { ["id"]=> NULL ["name_ka"]=> string(37) "ბაზალეთის ტბა"...
49th row:array(22) { ["id"]=> NULL ["name_ka"]=> string(59) "სახლ-მუზეუმი, მუზეუმი, "
我似乎无法找到问题所在。
答案 0 :(得分:0)
这是因为多个表中的id重复......
使用代码:
$this->db->select('locations.id as location_id, gallery.id as gallery_id');
答案 1 :(得分:0)
试试这个
模型
function get_location($id)
{
$this->db->select('locations.id, locations.name_ka, locations.text_ka, locations.keyword_ka, locations.region, locations.latitude, locations.longitude, locations.notes_ka, locations.description_ka, locations.recomendation, locations.img, locations.nature, locations.culture, locations.resort, regions.region_type, regions.region_name_ka, resort.resort_name_ka, nature.nature_name_ka, culture.culture_name_ka, gallery.id, gallery.post_id, gallery.img_link, gallery.gallery_name_ka')
->from('locations')
->join('regions', 'regions.id = locations.region','left')
->join('culture', 'culture.id = locations.culture','left')
->join('resort', 'resort.id = locations.resort','left')
->join('nature', 'nature.id = locations.nature','left')
->join('gallery', 'gallery.post_id = locations.id','left')
->where('locations.id',$id);
$queryo = $this->db->get();
return $queryo->row_array();
}