0

A strange issue. this is the array with some values

$et_er_facilities = ( [0] => Swimming pool 
[1] => Squash court 
[2] => Mini market 
[3] => Playground ) 

If I add a few checkboxes to check whose value is present in the array above, I am trying to use in_array function. The function works for first check, but on all the next checks, it doesn't work. Here is my code.

<input type="checkbox" id="facilities1" value="Swimming pool" name="et_er_facilities[]" <?php if (in_array("Swimming pool", $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities4" value="Squash court" name="et_er_facilities[]" <?php if (in_array('Squash court', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities5" value="Mini market" name="et_er_facilities[]" <?php if (in_array('Mini market', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities7" value="Playground" name="et_er_facilities[]" <?php if (in_array('Playground', $et_er_facilities)) {?>checked="checked"<?php }?>>

So this is how its working currently. If in the array above, the first value is Playground, the checkbox will select only Playground, no other it will select automatically.

In all cases, it just matches the first value of the array and selects the appropriate check box, but doesn't select the others if they match too.

Any help, will be highly appreciated.

Thanks

2 Answers 2

2

Initialize an array as

$et_er_facilities =  array(
    0 => 'Swimming pool',
    1 => 'Squash court', 
    2 => 'Mini market', 
    3 => 'Playground'
);
1
  • thanks. but how would I do that. the actual value in the db is a string. like "Swimming Pool, Mini market, Playground" it was converted in array when I did explode. Commented Apr 23, 2013 at 17:53
0

I modified your code

<html>
<body>
<?php
*$et_er_facilities = array("Swimming pool","Squash court","Mini market","Playground" );* // most prob this was wrong in ur code

?>
<form>
<input type="checkbox" id="facilities1" value="Swimming pool" name="et_er_facilities[]"         <?php if (in_array("Swimming pool", $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities4" value="Squash court" name="et_er_facilities[]" <?php if (in_array('Squash court', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities5" value="Mini market" name="et_er_facilities[]" <?php if (in_array('Mini market', $et_er_facilities)) {?>checked="checked"<?php }?>>
<input type="checkbox" id="facilities7" value="Playground" name="et_er_facilities[]" <?php if (in_array('Playground', $et_er_facilities)) {?>checked="checked"<?php }?>>
</form>
</body>
</html>
  1. Without SwimmingPool in array $et_er_facilities = array("Squash court","Mini market","Playground" ); and it gave 4 check boxes all checked except the 1st one

  2. With SwimmingPool in array $et_er_facilities = array("Swimming pool","Squash court","Mini market","Playground" ); and it gave all 4 check boxes checked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.