Question form with radio button returning showing empty

0

Good evening,  I need to ask a question system exactly like this:      link

I just need as many questions and answer options as come from the bank.  I tried this way only the radios are coming out empty, plus the query is ok, and running it in MySQL it  correctly returns the data, I think it might be something with the loop's, could someone help me to find the error?:

Following code:

     <?php
     //CONEXÃO
$servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "master";
        $conn = new mysqli($servername, $username, $password, $dbname);  
         if ($conn->connect_error) {
             die("Connection failed: " . $conn->connect_error);
         }
     //CONEXÃO

      $QPergunta  = "SELECT sug_perg_id AS ID_PERGUNTA, sug_perg_pergunta AS PERGUNTA FROM sugestoes_perguntas WHERE sug_perg_area = 3";
      $QAvalicao  = "SELECT sug_aval_id AS ID_AVALIACAO, sug_aval_desc AS DESCRICAO FROM sugestoes_avaliacao";
      $RPergunta  = $conn->query($QPergunta);
      $RAvaliacao = $conn->query($sql);
          while($row = $RPergunta->fetch_assoc()){
            echo "PERGUNTA :".$row["PERGUNTA"];
            echo"<br>";
            while($row = $RAvaliacao->fetch_assoc()){
             echo"<input type='radio' name='".$row["ID_PERGUNTA"]."' value='".$row["ID_AVALIACAO"]."'>".$row["DESCRICAO"];
              echo"<br>";
          }
      }

     //FINALIZA CONEXÃO

        $conn->close();

     //FINALIZA CONEXÃO
     ?> 
    
asked by anonymous 11.07.2017 / 00:23

1 answer

1

<?php
//CONEXÃO
   $servername = "127.0.0.1"; $username = "root"; $password = "root"; $dbname = "master";
$conn = new mysqli($servername, $username, $password, $dbname);  
if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error);
}
//CONEXÃO

$QPergunta  = "SELECT sug_perg_id AS ID_PERGUNTA, sug_perg_pergunta AS PERGUNTA FROM sugestoes_perguntas WHERE sug_perg_area = 3";
$QAvalicao  = "SELECT sug_aval_id AS ID_AVALIACAO, sug_aval_desc AS DESCRICAO FROM sugestoes_avaliacao";
$RPergunta  = $conn->query($QPergunta);
$RAvaliacao = $conn->query($QAvalicao);

$Repostas = [];
while($Repostas[] = $RAvaliacao->fetch_assoc());

while($row_pergunta = $RPergunta->fetch_assoc()){
	echo "PERGUNTA :".$row["PERGUNTA"];
	echo"<br>";
	foreach ($Repostas as $row_resposta) {
		echo"<input type='radio' name='".$row_pergunta["ID_PERGUNTA"]."' value='".$row_resposta["ID_AVALIACAO"]."'>".$row_resposta["DESCRICAO"];
		echo"<br>";
	}
}

//FINALIZA CONEXÃO

$conn->close();

//FINALIZA CONEXÃO
?>

This will probably solve your problem.

The variable $ sql does not exist, has been replaced by $ QAvalicao;

The second problem is as follows, you just made a bank call to pick up the answers. When the first while exits the first record it means that the second while (most internal) has already read all the records of answers, so after the first question there will be no answers.

So, in this way I've saved the response records in a variable:

$Repostas = [];
while($Repostas[] = $RAvaliacao->fetch_assoc());

And then for each question record I run this variable with foreach;

Third error, the variable $ row was repeating and when it was called by the second while in $ row ["ID_PERGUNTA"] it would return an empty value, because at that point of the code the variable $ row only had the data of the answer and not the question. For this I have renamed the variables to $ row_quest and $ row_response. Specifying the variable name name precisely with the name closest to it was guarded is a great practice of code. It helps you understand a code after a long time without seeing it and still helps in team projects.

Ps:

while($Repostas[] = $RAvaliacao->fetch_assoc());

This While is not empty, it is filling in the variable $ Answers with the records of the answers. Although the person in the comment below does not notice, the command within the while conditional is assigning the fetch to $ []. When using the expression [] in an array, php creates a new index and assigns the value to it. The assignment returns this value to the while conditional. when this assignment is null, the while to.

I hope I have helped.

    
11.07.2017 / 13:11