使用以下代码发送json格式作为输入并将其存储在db中。当我解码的价值我什么也没得到。我的代码出了什么问题。请有人帮帮我。
控制器
function join_community_post(){
$serviceName = 'join_community';
//getting posted values
$c = '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
$ip = trim($this->input->post($c));
$ipJson = json_decode($ip);
print_r ($ipJson);exit;
$retVals = $this->user_model->user_join_community($ip, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
模型
function user_join_community($ip,$serviceName) {
$ipJson = json_decode($ip);
}
答案 0 :(得分:0)
如果第二个参数未作为true传递,则json_decode会给出stdClass对象<{3}}
第二,你传递的是$ c的帖子值...检查它是否为空PHP:json_decode
控制器
function join_community_post(){
$serviceName = 'join_community';
//getting posted values
//$c = '{"community" : [{"community_id":1},{"community_id":2},{"community_id":3}]}';
$ip = trim($this->input->post($c));
$ipJson = json_encode($ip);
$retVals = $this->user_model->user_join_community($ip, $serviceName);
header("content-type: application/json");
echo $retVals;
exit;
}
模型
function user_join_community($c,$serviceName) {
$ipJson = json_decode($c,true);
$createArray = array();
foreach($ipJson['community'] as $key=>$value){
$createArray = array(
'user_community_created_date' => date('Y-m-d H:i:s'),
'user_community_modified_date' => date('Y-m-d H:i:s'),
'community_id' => $value['community_id']
);
$insert = $this->db->insert('user_community', $createArray);
}
if ($insert) {
$data['message'] = 'success';
$status = $this->ville_lib->return_status('success', $serviceName, $data, $c);
} else {
$data['message'] = 'Error';
$status = $this->ville_lib->return_status('error', $serviceName, $data, $c);
}
}
在foreach的大括号后删除分号,并将$ c添加到webservices表中,这会让你发现错误.....希望它有帮助......