Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过JSON(CodeIgniter)正确传递数据_Json_Codeigniter - Fatal编程技术网

如何通过JSON(CodeIgniter)正确传递数据

如何通过JSON(CodeIgniter)正确传递数据,json,codeigniter,Json,Codeigniter,我试图通过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

我试图通过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;
};

我应该怎么做,从数组中只获取一个元素?我希望我的国家/地区值是
$array['data']['country']
,但是如果我在控制器中键入它,我会得到(object object)

您已经将
$msg
数组中的'country'键设置为整个结果集数组

如果我正确理解了您试图执行的操作,那么国家值实际上将是
$msg[country][country]

如果在设置所使用的
$msg
数组
$array[data][country]
时,只需使用
$msg[country]
即可检索国家值,这会更好

我希望我了解你的问题所在


另一方面,将数组命名为“array”可能不是一个好主意,因为它可能会很快变得混乱。根据数组中的内容命名,例如结果

$array['data']->country
?否:严重性:注意

消息:尝试获取非对象的属性

文件名:controllers/records.php

行号:83

var\u Dump('array['data'])
,并查看它是什么类型以及如何从中获取值。正如我从json中看到的一样,它是对象数组。然后使用
$array['data'][0]->country
$array['data'][0]['country']
{"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"}]}