简体   繁体   中英

in codeigniter using rest api how to save the json data in table

I want to save the JSON data posted from android app to REST API in codeigniter .

The JSON data is:

controller

<?php defined('BASEPATH') OR exit('No direct script access allowed');

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';


class Test extends REST_Controller
{
    private $postData;
    private $getData;
    function __construct(){
        parent::__construct();
        header('Content-Type: application/json');
        #Check if Content Type is JSON
        if( isset( $_SERVER['CONTENT_TYPE'] ) && strpos( $_SERVER['CONTENT_TYPE'], "application/json" ) !== false ){      
            $jsonReqData = json_decode( trim( file_get_contents( 'php://input' ) ), true );
        }else{
            $jsonReqData = $this->input->post();
        }
        $this->postData = $jsonReqData;     
        $this->load->model('mymodel');
        $this->methods['user_get']['limit'] = 500; //500 requests per hour per user/key
        $this->methods['user_get']['limit'] = 100; //100 requests per hour per user/key
        $this->methods['user_delete']['limit'] = 50; //50 requests per hour per user/key
    }

    function save_post(){   
        $formData=array();
        $photo=trim($_POST['photo'],'"');
        $photo=filter_var($photo, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);   

        //$questions=trim($_POST['questions'],'"');
        //$formData['questions']=filter_var($questions, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $formData['questions']=$_POST['questions'];
        print_r($formData['questions']);die();

        $caseId=trim($_POST['case'],'"');
        $formData['case']=filter_var($caseId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $stateId=trim($_POST['stat'],'"');
        $formData['stat']=filter_var($stateId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $foe_tat=trim($_POST['tat'],'"');
        $formData['tat']=filter_var($foe_tat, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $applicantId=trim($_POST['applicant'],'"');
        $formData['applicant']=filter_var($applicantId, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        //$answer=trim($_POST['answer'],'"');
        //$formData['answer']=filter_var($answer, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $foe_user=trim($_POST['uid'],'"');
        $formData['foe_user']=filter_var($foe_user, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);

        $comment=trim($_POST['comment'],'"');
        $formData['comment']=filter_var($comment, FILTER_SANITIZE_STRING | FILTER_SANITIZE_MAGIC_QUOTES);
        $data_image=array();
        $data_image = explode(",",$photo);
        $imageList=array();
        for ($x = 0; $x < count($data_image); $x++) {
            $rand_num = rand();         
            $data_img_dec = base64_decode($data_image[$x]);
            #image name is combination of applicantId+rand+foe_user
            $img_filename = $applicantId."_".$rand_num.'_'.$foe_user.'.png';        
            $imgpath = APPPATH.'uploads/uploads/'.$img_filename;  #saving image
            $success = file_put_contents($imgpath, $data_img_dec);  
            $imageList[]=$img_filename;
        }
        $formData['site_photo']=implode(",",$imageList);
        //echo "<pre>";print_r($formData);
        $result = $this->mymodel->saveTest($formData);
        $this->response(array('status'=>'Success','data'=>$result),200);        
    }

{
    "tat": "6",
    "stat": "4",
    "comment": "",
    "uid": "2",
    "case": "8EZKq2QgJR",
    "photo": "",
    "applicant": "1230000",
    "questions": {
        "34": "No",
        "46": "fgbfb",
        "48": "NA",
        "29": "NA",
        "45": "dsd",
        "49": "InConclusive",
        "43": "1 BHK",
        "35": "NA",
        "38": "12",
        "39": "2",
        "27": "q1",
        "41": "Others",
        "52": "fgfdg",
        "47": "fgfg",
        "31": "Chawl",
        "33": "Upper Middle Class",
        "37": "Self Owned",
        "30": "fdgfdgb",
        "50": "fgfdgb",
        "51": "fgfdg",
        "32": "NA",
        "44": [
            "Refrigerator",
            "Airconditioner"
        ]
    }
}

From this I want to loop for questions to insert multiple rows in a table by taking all the values from the JSON data.

$myJson = file_get_contents("php://input");
$myArray = json_decode($myJson, true);

foreach($myArray['questions'] as $a){
   // save every question in db!
}

Just use this code::

<?php
$data = '{
    "tat": "6",
    "stat": "4",
    "comment": "",
    "uid": "2",
    "case": "8EZKq2QgJR",
    "photo": "",
    "applicant": "1230000",
    "questions": {
        "34": "No",
        "46": "fgbfb",
        "48": "NA",
        "29": "NA",
        "45": "dsd",
        "49": "InConclusive",
        "43": "1 BHK",
        "35": "NA",
        "38": "12",
        "39": "2",
        "27": "q1",
        "41": "Others",
        "52": "fgfdg",
        "47": "fgfg",
        "31": "Chawl",
        "33": "Upper Middle Class",
        "37": "Self Owned",
        "30": "fdgfdgb",
        "50": "fgfdgb",
        "51": "fgfdg",
        "32": "NA",
        "44": [
            "Refrigerator",
            "Airconditioner"
        ]
    }
}';
$data = (array) json_decode($data);
foreach($data as $key=>$value){
        if($key=="questions"){
            foreach($value as $row){
                // your save code here;
            }
        }
}
?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM