我在sub_regions
的基础上提取region_id
名称。
通过ajax完成它,所以在加入region和sub_region表之后,我将获得谷歌Chrome控制台的 network 中的结果。这意味着查询和其他一切都运行良好。
现在的问题是:
我得到的数据是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"}
答案 0 :(得分:0)
success: function (result) {
$.each(result, function(key, item) {
var get_result = "<p>"+item.name+"</p><br/>";
$(get_result).appendTo('#result');
});
}
OR
success: function (result) {
for (var i = 0; i < data.result; i++) {
var get_result = "<p>"+data[i].name+"</p> <br/>";
$(get_result).appendTo('#result');
}
}
检查出来