我正在尝试将数据从我的模型传递到控制器,并从我的控制器传递到通过 JSON 进行查看。
我的模型:
public function GetRecordDetails($array){
$this->db->select('*');
$this->db->where('type', $array['event_type']);
$this->db->where('id', $array['event_id']);
return $this->db->get('records')->result_array();
}
我的控制器:
public function show_record(){
$this->load->model('records_model');
$data = array(
"event_id" => $_POST['id'],
"event_type" => $_POST['type']
);
$msg = array();
if(empty($data['event_id'])) $msg['failed'] = "failed"; else
if(empty($data['event_type'])) $msg['failed'] = "failed"; else
if($this->records_model->CheckForRecord($data) == 0) $msg['failed'] = "failed"; else
if(count($msg) == 0){
$array['data'] = $this->records_model->GetRecordDetails($data);
$msg = array(
"success" => "success",
"id" => $data['event_id'],
"country" => $array['data']
);
}
echo json_encode($msg);
}
和我的观点:
EVENT.show = function (id, type) {
$("#loader_" + id).html("<img src='images/loaders/loader2.gif' width='11px' height='11px' alt='loading' />");
$("#failure").css('display', 'none');
$("#record_show").css('display', 'none');
var form_data = {
id : id,
type : type
}
$.ajax({
url: "records/show_record",
type: 'POST',
dataType: 'json',
data: form_data,
success: function (data) {
if(data.failed){
$("#loader_" + id).html("");
$("#failure").fadeIn(500);
$("#msg_area").html(data.failed);
}
if(data.success){
$("#loader_" + id).html("");
$("#record_show").fadeIn(500);
$("#record_id").val(data.id);
$("#record_country").val(data.country);
}
},
error: function (e) {
console.log(e.message);
}
});
return false;
};
我无法从控制器中的数组接收“国家”值。我只是不知道该怎么做。
它从我的 mysql 数据中为我返回所有数组:
{"success":"success","id":"38","country":[{"id":"38","type":"1","country":"1","event":"RAFAEL NADAL vs NOVAK DJOKOVIC","date":"2013-07-24 00:00:00","selection":"Novak Djokovic +2.5","odds":"1.83","result":"1"}]}
我应该怎么做,从我的数组中只抓取一个元素?我希望我的国家值是$array['data']['country']
,但如果我在控制器中输入它,我会得到 (object Object)