list of a php survey

Asked

Viewed 1,461 times

3

I have a questionnaire and need to pass a list with the question and the answer value, created a question table and a response table in my database, but I’m not able to pass the answer value in this list.

1. database

tabela pergunta ->

            idPergunta
            pergunta
            resposta


tabela resposta ->

        idResposta
        idUsuario fk idUsuario table usuario
        idQuestionario fk idQuestionario table questionario
        idpergunta fk idPergunta table pergunta
        valor

2. The php code is this:

    <?php include ("cabecalho.php");

      $result = getAllUsuarios();


      while($row = mysqli_fetch_array($result)) {
       ?><h3><?php echo $row['nome'];?></h3>
       <?php
           $id = $row['idusuario'];
           $resultRespostaUsuario = getRespostaByPerguntaByIdUsuario($id);
           $questionario = getAllQuestionario();

             while ($row2 = mysqli_fetch_array($questionario)) {
              $idQuestionario = $row2['idquestionario'];
              $resultPerguntaUsuario= getPerguntaByIdQuestionario($idQuestionario);
            ?><h4><?php echo $row2['nomeQuestionario']; ?> </h4>

                <table class="table table-bordered table-striped">
                    <tr>
                        <td><a>pergunta</a></td>
                        <td><a>resposta</a></td>
                    </tr>
                <?php
                while($row3 = mysqli_fetch_array($resultPerguntaUsuario)){
                ?>
                    <tr>
                        <td><?php echo $row3['pergunta'];?></td>

                <?php  
                while($row4 = mysqli_fetch_array($resultRespostaUsuario)) {
        ?>           
                        <td><?php echo $row4['valor'];?></td>
                     </tr>
                 <?php } 
            }

        ?>

      </table> 
    <?php } 
    } ?>

3. Duties:

     function getAllUsuarios(){
       $con = getConnection();
       $sql = "select * from usuario";
       return mysqli_query($con,$sql);}

     function getRespostaByPerguntaByIdUsuario($id){
       $con = getConnection();
       $sql = "select * from resposta r 
        inner join questionarioPergunta qp 
        on qp.idpergunta = r.idpergunta where idusuario = $id";}

     function getAllQuestionario(){
       $con = getConnection();
       $sql = "select q.*, user.nome nome from questionario q
        INNER JOIN usuario user
        ON q.idcriador=user.idusuario " ;
       return mysqli_query($con,$sql);}

     function getPerguntaByIdQuestionario($id){
       $con = getConnection();
       $sql = "select * from pergunta p inner join questionarioPergunta qp 
        on qp.idpergunta = p.idpergunta where idquestionario = $id";
       return mysqli_query($con,$sql);}
  • What does it mean "pass the response value in this list"? The problem is here while($row4 = mysqli_fetch_array($resultRespostaUsuario))? What’s the mistake? Did some debug with var_dump? I think it is difficult to say what the problem is because you cannot reproduce your code in another system (see MCVE).

  • means I want to show you the answer the user gave on a list. and a log error [:error] [pid 1371] [client 127.0.0.1:59409] PHP Warning: mysqli_fetch_array() expects Parameter 1 to be mysqli_result, null Given in /var/www/questionario/listResposta.php on line 30, referer: http://localhost/questionario/listaRespospostaUsuario.php? idquestionario=2

  • Please, [Edit]and the question to add new information.

  • I believe it is not possible to do this way with php because it is run only once (no load of page) I will post a response...

2 answers

4


The solution is this:

1-PHP code:

    <?php include ("cabecalho.php");

        $result = getuserById($_GET['idusuario']);

        ?> <br /><br /><br /><br />
    <?php
     while($row = mysqli_fetch_array($result)) {
        ?><h3><?php echo $row['nome'];?></h3>
        <?php
            $id = $row['idUsuario'];
            $resultRespostaUsuario = getRespostaByPerguntaByIdUsuario($id);
            $questionario = getQuestionarioById($_GET['idquestionario']);
                while ($row2 = mysqli_fetch_array($questionario)) {
                $idQuestionario = $row2['idquestionario'];
                $resultPerguntaUsuario = getPerguntaByIdQuestionario($idQuestionario);
                ?><h4><?php echo $row2['nomeQuestionario']; ?> </h4>

                    <table class="table table-bordered table-striped">
                        <tr>
                            <td><a>pergunta</a></td>
                            <td><a>resposta</a></td>
                        </tr>
                    <?php
                    while($row3 = mysqli_fetch_array($resultPerguntaUsuario)){
                    ?>
                        <tr>
                            <td><?php echo $row3['pergunta'];?></td>
                            <td><?php echo $row3['resposta'];?></td>
                        </tr>
                    <?php }
                }

            ?>

</table> 
<br><br><br>
    <?php  
} 
include ("rodape.php"); ?>

2-Functions:

    function getUserById($id){
    $con = getConnection();
    $sql = "select idusuario as idUsuario,
            nome,telefone,nasc,endereco,email,login,senha from usuario where idusuario = $id";
    return mysqli_query($con,$sql);
}
    function getRespostaByPerguntaByIdUsuario($id){
    $con = getConnection();
    $sql = "select * from resposta r 
    inner join questionarioPergunta qp 
    on qp.idpergunta = r.idpergunta where idusuario = $id";
}
    function getQuestionarioById($id){
    $con = getConnection();
    $sql = "select * from questionario where idquestionario = $id";
    return mysqli_query($con,$sql);
}
    function getPerguntaByIdQuestionario($id){
    $con = getConnection();
    $sql = "select * from pergunta p inner join questionarioPergunta qp on qp.idpergunta = p.idpergunta where idquestionario = $id";
    return mysqli_query($con,$sql);
}
  • Cool, now after two days you can mark your own response as sure :) +1

2

I made some changes to the code, first I take the values and then I pass them to html

<?php include ("cabecalho.php");

  $result = getAllUsuarios();

  $nome = "";
  $nomeQuestionario="";
  $pergunta="";
  $valor="";
  while($row = mysqli_fetch_array($result)) 
  {
      $id = $row['idusuario'];
       $resultRespostaUsuario = getRespostaByPerguntaByIdUsuario($id);
       $questionario = getAllQuestionario();
       $nome=$row['nome'];
       while ($row2 = mysqli_fetch_array($questionario))
       {
            $idQuestionario = $row2['idquestionario'];
            $resultPerguntaUsuario= getPerguntaByIdQuestionario($idQuestionario);
            $nomeQuestionario=$row2['nomeQuestionario'];
            while($row3 = mysqli_fetch_array($resultPerguntaUsuario))
            {
                $pergunta=$row3['pergunta'];
                while($row4 = mysqli_fetch_array($resultRespostaUsuario)) 
                {
                     $valor=$row4['valor'];  
                }
           }
        }    
  }
   ?>

   <h3><?php echo $nome;?></h3>
   <h4><?php echo $nomeQuestionario; ?> </h4>
   <table class="table table-bordered table-striped">
       <tr>
               <td><a>pergunta</a></td>
               <td><a>resposta</a></td>
       </tr>
       <tr>
               <td><?php echo $pergunta;?></td>
               <td><?php echo $valor;?></td>
       </tr>
  </table> 
  • in fact I already managed to solve yesterday, even so thank you Silvio :)

  • Ok @any_wolf would please post the solution so that other people with the same problem that you have answered, thank you..

Browser other questions tagged

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