Jump to content

Simple in_array() question


Twitch

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/235780-simple-in_array-question/
Share on other sites

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

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.