list of a questionnaire in php

3

I have a questionnaire and I need to pass a list with the question and the value of the answer, I created a question table and an answer table in my database, but I am not able to pass the value of the answer 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 code in php 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. Functions:

     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);}
    
asked by anonymous 08.07.2014 / 17:03

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);
}
    
11.07.2014 / 17:35
2

I made some changes to the code, first I get the values and then I pass them to the 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> 
    
10.07.2014 / 17:42