Questions form with radio button returning empty displaying

Asked

Viewed 123 times

0

Good night, I need to ask a question system (a question) just like this: https://jsfiddle.net/hmana2sL/

I just need the questions and the answer options to come from the bank. I tried this way only that the radios are coming out empty, the more the query this ok, and running it in Mysql returns the data correctly, think it might be something with the loops, someone could help me find the error?:

Follows 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
     ?> 

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, was exchanged for $Qavalicao;

The second problem is this, you only made one bank call to get the answers. When the first while leaves the first record it means that the second while (innermost) has already read all the answer records, then after the first question, there will be no answers.

So, this way I saved the answer 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 $Row variable was repeating itself and when it is called by the second while in $Row["ID_PERGUNTA"] it would return an empty value, because at this point in the code the $Row variable only had the answer data and not the question. For this I renamed the variables to $row_question and $row_answer. Specifying the variable face name precisely with the name closer to that of a guard is a great code practice. Helps you understand code after a long time without seeing it and still helps in team projects.

Ps:

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

While this is not empty, it is filling the variable $Answers with the records of the answers. Although the person in the comment below does not notice, the while conditional command is assigning fetch to $Repostas[]. 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 that assignment is null, while for.

I hope I’ve helped.

  • Why is this possibly going to solve the problem? Note that Sopt is not a support system, but a community for professional programmers and enthusiasts. The main goal is to create quality content that serves as a reference. A code-only answer goes far beyond that, so please edit your answer explaining what was wrong in the question code and what solution was adopted, if it is the only solution, if it is the best solution, why it is the best, etc. In fact, why a while empty to create the answer list? This seems completely unnecessary.

  • Okay, thank you, editing...

  • @Andersoncarloswoss Edited, and explained why the while "empty". Kisses.

  • Much better. Take this as a standard for future responses. By the way, the while remains empty. The fact that the condition performs something does not change the fact that it is empty. Remembering that emptiness is not the same as "doing nothing". For this case, you can use the function mysqli_fetch_all, that is optimized internally.

Browser other questions tagged

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