Give a value for radio input not selected

Asked

Viewed 134 times

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 the for. The use of switch also compared to if in this case is more appropriate, can you make the filtration better and more org

3 answers

3


Friend I believe that if you do this way is more efficient:

foreach ($_POST["respostas"] as $value) {
    switch (trim($value)) {
        case "":
        case null:
            $valor = 0;
            break;
        default:
            $valor = 1;
    }
    echo $valor;
}

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 the for.

The use of switch also compared to if in this case is more appropriate, can you make the filtration better and more organized.

I used the trim to avoid spaces at the beginning and end being added, an example that could come out is this: " "

0

Both do as follows below using condition structure if one-line, to select radio input:

<input type='radio' <?php if ($opcao == '1') echo 'checked'; ?> name='respostas[".$c."]' value='true'>" . "<br>";

Or also more simply using thermal operator:

<input type='radio' <?php echo $opcao == '1' ? 'checked' : ''; ?> name='respostas[".$c."]' value='true'>" . "<br>";

Remembering that to work any of the above two solutions, the variable $opcao has to be the booleana (true or false).

  • Hello Yure. I tried to use the way you mentioned, but is leaving marked all last selected option.

  • The $option variable contains what type of value? You could compare it in the Boolean count, for example: if ($option == '1') echo 'checked';

  • Hello Yure. You’re right. I forgot to put the values of the $option. I edited my doubt. It returns A,B,C,D and E.

0

Colleagues. I got it. The correct answer follows below:

for($contar = 0; $contar <= 9; $contar++){
   if($_POST["respostas"][$contar] != ""){
     $valor = "1";
   }else{
     $valor = "0";
   }
  echo $valor;
}

In this way, it sorts correctly and shows the values according to their completion. Ex.:

1
0
0
1
0
0

Browser other questions tagged

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