Load checkbox explodes

Asked

Viewed 66 times

0

I made a code with explode to carry checkbox of banco, however, it is only loading when it contains data in the exact order of variables, for example 'Theater 1, Theater 2' carries, but 'Theater 1, Baby Class' does not carry any...

Although I have designed for only two options I made a test with a insert of three, out of order: 'Theatre 2, Theatre 1, Dance 1', carried only 'Dance 1''

 <?php $Oficina == $Oficina_Explode;
 $Oficina_Explode = explode(', ', $Oficina);
 $Oficina_Explode[0]; //ganha o valor 'Teatro 1'
$Oficina_Explode[1]; //ganha o valor 'Teatro 2
$Oficina_Explode[2]; //ganha o valor 'Dança 1'
$Oficina_Explode[3]; //ganha o valor 'Dança 2'
$Oficina_Explode[4]; //ganha o valor 'Baby Class'
?>
    <meta charset="utf-8">
    <label class="container">Teatro 1 <input class='shared' type='checkbox' value="Teatro 1" name="Oficina[]" required="required" <?php if($Oficina_Explode[0] == 'Teatro 1') echo 'checked';?> /><span class="checkmark"></span></label>
    <label class="container">Teatro 2 <input class='shared' type='checkbox' value="Teatro 2" name="Oficina[]" required="required" <?php if($Oficina_Explode[1] == 'Teatro 2') echo 'checked';?> /><span class="checkmark"></span></label>
    <label class="container">Dança 1 <input class='shared' type='checkbox' value="Dança 1" name="Oficina[]" required="required" <?php if($Oficina_Explode[2] ==   'Dança 1') echo 'checked';?> /><span class="checkmark"></span></label>
    <label class="container">Dança 2 <input class='shared' type='checkbox' value="Dança 2" name="Oficina[]" required="required" <?php if($Oficina_Explode[3] == 'Dança 2') echo 'checked';?> /><span class="checkmark"></span></label>
    <label class="container"> Baby Class<input class='shared' type='checkbox' value="Baby Class" name="Oficina[]" required="required" <?php if($Oficina_Explode[4] == 'Baby Class') echo 'checked';?> /><span class="checkmark"></span></label>

How can I increment my code to solve this problem?

  • Confirmed if the value stored in the array is really the string you want?

  • I did the tests, I just saw that it’s not about accent... it’s only loading when it contains data in the exact order of variables, for example 'Theater 1, Theater 2' carries, but 'Theater 1, Baby Class' does not carry any...

  • If you entered with 'Teatro 2, Teatro 1, Dança 1', what guarantees you that $Oficina_explode[0] will have the value "Teatro 1", $Oficina_explode[1] will have the value "Teatro 2"? I imagine you’re missing information

1 answer

0


Solved. Thank you for the comments that helped me to arrive at the solution.

I created a if to check each checkbox, and thus fill them correctly.

    <?php $Oficina == $Oficina_Explode;
 $Oficina_Explode = explode(', ', $Oficina);
 $Oficina_Explode[0]; //ganha o valor 'Teatro 1'
$Oficina_Explode[1]; //ganha o valor 'Teatro 2
$Oficina_Explode[2]; //ganha o valor 'Dança 1'
$Oficina_Explode[3]; //ganha o valor 'Dança 2'
$Oficina_Explode[4]; //ganha o valor 'Baby Class'
if(in_array('Teatro 1', $Oficina_Explode)){
 echo "<label class='container'>Teatro 1 <input class='shared' type='checkbox' value='Teatro 1' name='Oficina[]' required='required' checked /><span class='checkmark'></span></label>";
}
else {
      echo "<label class='container'>Teatro 1 <input class='shared' type='checkbox' value='Teatro 1' name='Oficina[]' required='required' /><span class='checkmark'></span></label>";
}
if(in_array('Teatro 2', $Oficina_Explode)){
echo "<label class='container'>Teatro 2 <input class='shared' type='checkbox' value='Teatro 2' name='Oficina[]' required='required' checked /><span class='checkmark'></span></label>";
}
else{
      echo "<label class='container'>Teatro 2 <input class='shared' type='checkbox' value='Teatro 2' name='Oficina[]' required='required' /><span class='checkmark'></span></label>";
}
if(in_array('Dança 1', $Oficina_Explode)){
echo "<label class='container'>Dança 1 <input class='shared' type='checkbox' value='Dança 1' name='Oficina[]' required='required' checked /><span class='checkmark'></span></label>";
}
else
{
    echo "<label class='container'>Dança 1 <input class='shared' type='checkbox' value='Dança 1' name='Oficina[]' required='required' /><span class='checkmark'></span></label>";  
}
if(in_array('Dança 2', $Oficina_Explode)){
echo "<label class='container'>Dança 2 <input class='shared' type='checkbox' value='Dança 2' name='Oficina[]' required='required' checked /><span class='checkmark'></span></label>";
}
else{
      echo "<label class='container'>Dança 2 <input class='shared' type='checkbox' value='Dança 2' name='Oficina[]' required='required' /><span class='checkmark'></span></label>";
}
if(in_array('Baby Class', $Oficina_Explode)){
echo "<label class='container'> Baby Class<input class='shared' type='checkbox' value='Baby Class' name='Oficina[]' required='required' checked /><span class='checkmark'></span></label>";
}
else{
      echo "<label class='container'> Baby Class<input class='shared' type='checkbox' value='Baby Class' name='Oficina[]' required='required' /><span class='checkmark'></span></label>";
}
?>

Browser other questions tagged

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