am using the following code for sending json format as input and to store it in db. when i decode the value am getting nothing. what was wrong in my code. can someone help me please.
controller
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;
}
model
function user_join_community($ip,$serviceName) {
$ipJson = json_decode($ip);
}
The json_decode gives the stdClass Object if second parameter is not passed as true PHP:json_decode
And second you are passing the post value of $c...check if its blank Input Class
Controller
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;
}
Model
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);
}
}
Removed semicolon after curly bracket of foreach and added the $c to the webservices table which was getting you the error.....hope it helps...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.