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

2 番目のパラメーターが 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 に答える