List the answers according to the number of questions

Asked

Viewed 35 times

0

Colleagues.

I have the following code:

$array = array("","A","B","C","D","E");

for($contar = 1; $contar <= 10; $contar++){

}

Each $count corresponds to the number of questions. Ex.: 1 = Who was born first. 2 = Who discovered Brazil...

I would like it to be as follows:

    "Pergunta 1";
    "A: <input type='radio' name='respostas[]' value='A'>";
    "B: <input type='radio' name='respostas[]' value='B'>";
    "C: <input type='radio' name='respostas[]' value='C'>";
    ....
"Pergunta 2";
    "A: <input type='radio' name='respostas[]' value='A'>";
    "B: <input type='radio' name='respostas[]' value='B'>";
    "C: <input type='radio' name='respostas[]' value='C'>";

I’m kind of lost in the().

  • I didn’t understand very well, each question will have the whole alphabet of options?

  • Hello Igor. The alphabet goes from A to E. There will only be 5 answers. I will edit my question

2 answers

1

Try it like this:

for($contar = 1; $contar <= 10; $contar++){
    echo" Pergunta $contar ";
    for($i = 1; $i < count($array); $i++){
        echo"$array[$i]:<input type='radio' name='respostas[]' value='$array[$i]'> ";
    }
} 
  • 1

    Thank you all. The two options worked.

0


You need 2 fors what you already have to iterate the question and another internally to iterate the answer options!

$array = array("A","B","C","D","E");
    for($contar = 1; $contar <= 10; $contar++){
        echo "Pergunta " . $contar . "<br>";
        foreach ($array as $opcao) {
            echo $opcao . ": <input type='radio' name='respostas[]' value='" . $opcao ."'>" . "<br>";
        }
    }

Internally I used a foreach that I find easier to iterate arrays.

Browser other questions tagged

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