在codeigniter中通過id獲取數據我正在基於region_id
獲取sub_regions
名稱。 通過ajax做到這一點,所以加入區域和sub_region表後,我得到了谷歌Chrome控制檯的網絡中的結果。這意味着查詢和其他一切工作都很好。無法通過json
現在的問題是:
- 的數據不打印指定Div標籤
我得到的數據是所有
sub_regions
,它不是ID具體sub_region
的JavaScript :
<script> $("li a").click(function() { var x = $(this).attr('id'); // this is getting the id of the region from region links if (x.length > 0) { $.ajax({ type: "POST", data: "id=" + x, url: "http://localhost/sitename/autocomplete/GetRegionName", dataType: "json", success: function(result) { $.each(result, function(i, v) { // For each record in the returned array $('#result').append(v.name); // specified name as to display only the name of sub_region, not everything else alert(v.name); }); } // end success }); // end ajax } // if condition ends return false; }); </script>
PHP:
<?php
// This is the controller
public function GetRegionName(){
$keyword=$this->input->post('keyword');
$data=$this->regioncomplete->GetRow($keyword);
echo json_encode($data);
}
// This is the Model where i have joined 2 tables "regions" and "subregions" and declared that region ID = Sub region ID
class Regioncomplete extends CI_Model{
public function GetRow($keyword) {
$this->db->select('*');
$this->db->from('regions');
$this->db->join('sub_regions', 'regions.id = sub_regions.region_id');
$this->db->order_by('sub_regions.id', 'DESC');
$this->db->like("sub_regions.name", $keyword);
return $this->db->get()->result_array();
}
}
?>
我得到的結果網絡這樣
{id: "2", code: "CF", name: "California", country_id: "1", region_id: "2"}
{id: "1", code: "ST", name: "Seattle", country_id: "1", region_id: "1"}
data:{'keyword':x}也許? –
@歐津柏不知道那會是什麼...那會做什麼? –
哦對不起〜我沒有看到你得到的結果,它看起來像你有一個PHP腳本的反應,我認爲你應該檢查你''在'v'使用typeof(),並且它也打印出(v.name) –