Twitch Posted May 7, 2011 Share Posted May 7, 2011 I have checkboxes (one for each day of the week) that I want to mark checked if the value is in an array. The value of $row['specialDaily'] could be something like 1,2,0 or 1 or 2,3,6 etc. <?php $weekdays = array($row['specialDaily']);?> <input name="monday" type="checkbox" id="monday" value="1" class="weekday" <?php if (in_array("1", $weekdays)) { echo "checked='checked'"; }?> /><input name="tuesday" type="checkbox" id="tuesday" value="2" class="weekday" <?php if (in_array("2", $weekdays)) { echo "checked='checked'"; }?> /> If the array only contains one value, the proper checkbox is checked. However, if it contains more than 1 value no checkboxes are checked. I just found the in_array() function and assumed it was exactly what I'm looking for. I assumed that since $row['specialDaily']'s value is like an array I could just plug it in. Unfortunately, (unless I'm missing something simple) I'm thinking it doesn't work the way I hoped. Thanks in advance, Twitch Quote Link to comment https://forums.phpfreaks.com/topic/235780-simple-in_array-question/ Share on other sites More sharing options...
MadTechie Posted May 7, 2011 Share Posted May 7, 2011 Humm.. this doesn't look right to me <?php $weekdays = array($row['specialDaily']);?> if $row['specialDaily'] is "1,2,3" that will create an array with 1 key, ie Array("1,2,3") try this instead explode <?php $weekdays = explode(",",$row['specialDaily']);?> should create Array("1","2","3") Hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/235780-simple-in_array-question/#findComment-1211969 Share on other sites More sharing options...
Twitch Posted May 7, 2011 Author Share Posted May 7, 2011 Thanks for the incredibly fast reply. I see what you're saying so I assumed I needed to do it like this: <?php $days = explode(",",$row['specialDaily']);?> <?php $weekdays = array($days[0]);?> However that doesn't seem to work either. Thanks, Twitch Quote Link to comment https://forums.phpfreaks.com/topic/235780-simple-in_array-question/#findComment-1211972 Share on other sites More sharing options...
Twitch Posted May 7, 2011 Author Share Posted May 7, 2011 my mistake, I needed to use exactly what you wrote...haha Thanks, it's working! Quote Link to comment https://forums.phpfreaks.com/topic/235780-simple-in_array-question/#findComment-1211974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.