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
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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.