Check Checkboxes dynamically
Posted
by Selom
on Stack Overflow
See other posts from Stack Overflow
or by Selom
Published on 2010-05-07T10:09:06Z
Indexed on
2010/05/07
10:18 UTC
Read the original article
Hit count: 479
php
Hi, ive been dealing with this for some time now and need your help. well I have an array $arrayAmenities which contains a combination of the following data based on what is fetched from the database:
Air Conditioned
Bar
Brunch
Party Room
Tea Room
Terrace
Valet
I would like the application to dynamically check the following set of checkboxes based on the data contained in the array. With my code only one checkbox is checked based on the first data contained in the array.
Can you please tell what Im missing? Thanks for answering.
code:
//get amenities one by one in order to set the checkboxes
$arrayAmenities = explode(',', $rest_amenities );
$i=0;
while(count($arrayAmenities) > $i)
{
var_dump($arrayAmenities[$i]);
switch($arrayAmenities[$i])
{
case 'Air Conditioned':
$checkedAir = 'checked=true';
break;
case 'Bar':
$checkedBar = 'checked=true';
break;
case 'Brunch':
$checkedBru = 'checked=true';
break;
case 'Party Room';
$checkedPar = 'checked=true';
break;
}
$i+=1;
}
}
checkboxes
<table cellpadding="0" cellspacing="0" style="font-size:10px">
<tr>
<td style="border-top:1px solid #CCC;border-right:1px solid #CCC;border-left:1px solid #CCC; padding-left:5px ">Air Conditioned <input type="checkbox" name="air_cond" <?php print $checkedAir;?> value="Air Conditioned"></td>
<td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC;">Bar <input type="checkbox" name="bar" value="Bar" <?php print $checkedBar;?>></td>
<td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; ">Brunch <input type="checkbox" name="brunch" value="Brunch" <?php print $checkedBru;?>></td>
</tr>
<tr>
<td style="border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC; border-left:1px solid #CCC; padding-left:5px">Party Room <input <?php print $checkedPar;?> type="checkbox" name="party_room" value="Party Room" ></td>
<td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC;">Tea Room <input type="checkbox" name="tea_room" value="Tea Room" ></td>
<td style="padding-left:10px; border-top:1px solid #CCC;border-right:1px solid #CCC; border-bottom:1px solid #CCC;">Terrace <input type="checkbox" name="terrace" value="Terrace"></td>
</tr>
<tr>
<td colspan="3" style="border-bottom:1px solid #CCC; border-left:1px solid #CCC; border-right:1px solid #CCC; padding-left:5px">Valet <input type="checkbox" name="valet" value="Valet" ></td>
</tr>
</table>
© Stack Overflow or respective owner