Reading array and saving with php

Asked

Viewed 55 times

1

I am trying to save the result of this Json in Php but when I enter the answers foreach 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          
 }

inserir a descrição da imagem aqui

  • 1

    Give more details. Which error exactly occurs?

  • 1

    I added a photo of how it is allocating in the database, occurs that if I have more than one answer to a question my code is saving so and not all answers to all questions.

  • Dude, what’s this if checking array with foreach inside?

  • 1

    was validating whether the answer was an array but now that I saw that it will always be an array even if it only contains an answer in it

  • Yeah, the error should be there, you’re wanting to save the array in the bank

1 answer

1


I turned the Answers array into a json for you to save in 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 the json in the database, do so:

$resposta = inplode(",",$val['Resposta']);
  • 1

    Wow, that was great! Just one question, in this excerpt "$reply = addslashes(json_encode($val['Reply']));" he is saving in the database "["Macintosh OSX","Linux"], how can I be saved this way? " Macintosh OSX, Linux" separated by comma?

  • is because it’s a json, right, when you need it and pull from the bench, in the answers field you use json_decode and will turn an array again

  • @Carloslopes hard is the gnt come give the answer and have gnt q comes signal negatively, will understand

  • 1

    is that it does not need to be pulled from the bank as a json, in this case when I perform the select in this table I recreate the result in a json, so I needed to save in a line separated by comma.

  • 1

    already helped me a lot!!! Really Thank you Friend I will research a little more Aki!!! Thanks!!!

  • edited the answer

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.