1
Colleagues.
I have the following code:
$array = array("A","B","C","D","E");
for($contar = 1; $contar <= 9; $contar++){
echo "Pergunta " . $contar . "<br>";
foreach($array as $opcao) {
echo $opcao . ": <input type='radio' name='respostas[".$contar."]' value='" . $opcao ."'>" . "<br>";
}
}
And I’m taking values that way:
for($contar = 1; $contar <= 9; $contar++){
foreach($respostas as $resposta){
if(!empty($resposta[$contar])){
$valor = "1";
}else{
$valor = "0";
}
}
}
}
I need to make the fields that are not selected receive the value 0, but when I fill the fields, it triples and is not in the correct order.
Friend I believe that if you do it this way is more efficient: foreach ($_POST["answers"] as $value) { switch (Trim($value)) { case "": case null: $value = 0; break; default: $value = 1; } echo $value; } The reasons are for
foreach
be more appropriate for this, it in case will do the verification of all automatically, even if new values are added you do not need to make the modification in the code, which in case would be necessary with the use of thefor
. The use ofswitch
also compared toif
in this case is more appropriate, can you make the filtration better and more org– José