0

我正在使用以下代码发送 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);
    }
4

1 回答 1

0

如果第二个参数未作为 true PHP:json_decode传递,则 json_decode 会给出 stdClass 对象

其次,您正在传递 $c 的 post 值...检查其是否为空白输入类

控制器

   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 表中,这给您带来了错误......希望它有帮助......

于 2013-09-05T06:07:55.247 回答