List the answers according to the number of questions

0

Colleagues.

I have the following code:

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

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

}

Each $ count is the number of questions. Eg: 1 = Who was born first. 2 = Who discovered Brazil ...

I would like it to look like this:

    "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 for ().

    
asked by anonymous 14.05.2016 / 23:12

2 answers

0

You need 2 fors what you already have to iterate the question and another internally to iterate the response 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 it easier to iterate arrays .

    
15.05.2016 / 00:33
1

Try 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]'> ";
    }
} 
    
15.05.2016 / 00:29