Assign dynamic names

Asked

Viewed 44 times

0

I have a test in my database, and each question has its answers (Right, Wrong1, Wrong2, Wrong3). I can list the questions in different forms, but my problem is to list the answers in radiobuttons with different names. Since if 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>"; 

                                ?>
  • You cannot use: question_id_resposta_id?

  • what do you mean by that ?

  • From what I understand, the answers are getting duplicated, resp_1, resp_2, correct? Example: <input type='radio' name='perg_1_resp_1' />

  • Yes exact. And I was wondering if there’s a way I can automatically assign " name=pertg1_rest1", "name=pertg1_rest2", "name=pertg1_rest3", "name=pertg2_rest1" and so on

  • as I am removing the answers and questions from the database

  • But if you put them with the same name you will be able to dial more and a radio button, not?

Show 1 more comment

1 answer

0

Yes exact. And I was wondering if there’s a way 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 ames to the options it makes no sense to use input radio, because they can be marked more than one at the same time

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

In this example you are not getting the option from anywhere, 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;
  • It doesn’t work :/ I have the wrong answers in the same table. which are Answerrood1, Answerrood2, Answerrood3.. And yet I can’t create a group that can only select the answers to that question..

Browser other questions tagged

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