Assign dynamic names

0

I have a test in my database, and every question has its answers (Right, Wrong1, Wrong2, Wrong3). I can list the questions in different forms, but my problem is to list responses in radiobuttons with different names. Since everyone has the same name, when selecting one, the others will be disregarded.

    <?php

        $instS='Select * from perguntas';
        $query = mysqli_query($conn,$instS);

         while ($row = mysqli_fetch_assoc ($query))
         {echo"<br>";
                echo"<center><fieldset style='border:solid; width:500px;'>";

                  echo" <legend style='background: #FF9; width:150px; border: solid 1px black; 
                                -webkit-border-radius: 8px; -moz-border-radius: 8px; 
                                border-radius: 8px; padding: 6px;'> Questão: ".$row['idPergunta']."";echo"</legend>";

             echo "<center> <h4><b> Pergunta: </h4></center></b>";

        echo"<center>"
            .$row['textoPerguntas'].""; //Buscar a pergunta a base de dados
         echo"<center>";

        echo "<center><h4><b> Resposta: </h4></center></b>";

        // queria aqui buscar as respostas, mas de modo a que a cada set de 4    perguntas, os nomes das radiobuttons fossem alterados (RespostaCerta,Errada1,Errada2,Errada3 name=questao1), 
(RespostaCerta,Errada1,Errada2,Errada3 name=questao2), etc, consoante ao numero de perguntas que existe

        echo "<input type='radio' name='dd' id='resposta'>";

        echo "<br>";
        echo "<br>"; 

                                ?>
    
asked by anonymous 16.01.2017 / 14:47

1 answer

0
  

Yes accurate .. And I wanted to know if you have how I can automatically assign "name = pertg1_rest1", "name = pertg1_rest2", "name = pertg1_rest3", "name = pertg2_rest1" and so on

You are using input radio , if you assign different names to the options it does not make sense to use input radio , because they can be marked more than one at a time

echo "<input type='radio' name='dd' id='resposta'>";

In this example you are not taking the option of no place, so you can simply change your names manually:

<?php
$instS='Select * from perguntas';
$query = mysqli_query($conn,$instS);
while ($row = mysqli_fetch_assoc ($query)):
    $radioname = "pergunta-".$row['idPergunta'];
    $retorna = '<br>'
        ."<center><fieldset style='border:solid; width:500px;'>"
        ."<legend style='background: #FF9; width:150px; border: solid 1px black -webkit-border-radius: 8px; -moz-border-radius: 8px; ; border-radius: 8px; padding: 6px;'"
        ."Questão: ".$row['idPergunta']
        ."</legend>"
        ."<center> <h4><b> Pergunta: </h4></center></b><center>".$row['textoPerguntas']."<center>"
        ."<center><h4><b> Resposta: </h4></center></b>"
        ."<input type='radio' name='".$radioname."-1' id='resposta-1'> Opção 1"
        ."<input type='radio' name='".$radioname."-2' id='resposta-2'> Opção 2"
        ."<input type='radio' name='".$radioname."-3' id='resposta-3'> Opção 3"
        ."<input type='radio' name='".$radioname."-4' id='resposta-4'> Opção 4"
        ."<br/><br/>";
    echo $retorna;
endwhile;
    
16.01.2017 / 15:39