Reading array and saving with php

1

I am trying to save the result of this Json in Php but when I enter the foreach of the answers the data is incorrect in the database.

{"questionario":[{"id_base":"661","id_usuario":"1","id_pesquisa":"29","id_varejo":"265285","respostas":[{"Pergunta":"OS","Resposta":["Windows","Linux","Macintosh OSX"]},{"Pergunta":"Plese select from the list","Resposta":["PHP","C#","Ruby","Shell","Objective-C"]}],"data_inicio":"2018-05-08 09:53:08"}]}





public function saveResult($post) {
    $array = $post;
    foreach ($array['questionario'] as $value) {
                $iduser      = $value['id_usuario'];
                $idPesquisa  = $value['id_pesquisa'];
                $idVarejo    = $value['id_varejo'];
                $idBase      = $value['id_base'];
                $data_inicio = $value['data_inicio'];
                $respostas   = $value['respostas'];
                foreach($respostas as $val){
                    $pergunta = $val['Pergunta'];
                    $resposta = $val['Resposta'];
                    if(is_array($resposta)){
                        foreach($resposta as $resp){
                           $resp;
                        }    
                    }else{
                        $resp = $val['Resposta'];
                    }

            try{
            $db = Database::conexao();
            $qry = "INSERT INTO 'resultado'('id_pesquisa','id_varejo','id_usuario','id_base','pergunta','resposta','data_inicio')VALUES(
              :idPesquisa,
              :idVarejo,
              :iduser,
              :idbase,
              :pergunta,
              :resposta,
              :data_inicio)";

            $statement = $db->prepare($qry);
            $statement->bindValue(':idPesquisa', $idPesquisa);
            $statement->bindValue(':idVarejo', $idVarejo);
            $statement->bindValue(':iduser', $iduser);
            $statement->bindValue(':idbase', $idBase);
            $statement->bindValue(':pergunta', $pergunta);
            $statement->bindValue(':resposta', $resp);
            $statement->bindValue(':data_inicio', $data_inicio);
            $statement->execute();    

         } catch (PDOException $e){
                 echo $e->getMessage();
                 $db->rollBack(); 
         }        
         }//2 foreach    
    }//1 foreach          
 }

    
asked by anonymous 08.05.2018 / 15:00

1 answer

1

I've transformed the Response array into a json to save to the database:

public function saveResult($post) {
    $array = $post;
    foreach ($array['questionario'] as $value) {
                $iduser      = $value['id_usuario'];
                $idPesquisa  = $value['id_pesquisa'];
                $idVarejo    = $value['id_varejo'];
                $idBase      = $value['id_base'];
                $data_inicio = $value['data_inicio'];
                $respostas   = $value['respostas'];
                foreach($respostas as $val){
                    $pergunta = $val['Pergunta'];
                    $resposta = addslashes(json_encode($val['Resposta']));
                }
            try{
                    $db = Database::conexao();
                    $qry = "INSERT INTO 'resultado'('id_pesquisa','id_varejo','id_usuario','id_base','pergunta','resposta','data_inicio')VALUES(
                      :idPesquisa,
                      :idVarejo,
                      :iduser,
                      :idbase,
                      :pergunta,
                      :resposta,
                      :data_inicio)";

                    $statement = $db->prepare($qry);
                    $statement->bindValue(':idPesquisa', $idPesquisa);
                    $statement->bindValue(':idVarejo', $idVarejo);
                    $statement->bindValue(':iduser', $iduser);
                    $statement->bindValue(':idbase', $idBase);
                    $statement->bindValue(':pergunta', $pergunta);
                    $statement->bindValue(':resposta', $resp);
                    $statement->bindValue(':data_inicio', $data_inicio);
                    $statement->execute();

                } catch (PDOException $e){
                    echo $e->getMessage();
                    $db->rollBack(); 
                }
         }//2 foreach    
    }//1 foreach          
}

If you do not want json in the database, do so:

$resposta = inplode(",",$val['Resposta']);
    
08.05.2018 / 15:32