ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Checkbox Custom Field Conditionals

October 20, 2011 12:34am

Subscribe [2]
  • #1 / Oct 20, 2011 12:34am

    briankalwat

    50 posts

    Hi!

    So I have a custom field in one of my channels that has 7 checkboxes - one for each day of the week. The output of the custom field {job_schedule} is parsed as “Sunday|Tuesday|Friday” etc.. depending on which boxes are checked.

    I want to display specific HTML based upon these individual options, but the only way I could think of to do this was using strpos..

    Is there a better way?

    Here’s what i’ve got….

    <?php
    
    $job_schedule = '{job_schedule}';
    $sunday   = 'Sunday';
    $monday   = 'Monday';
    $tuesday   = 'Tuesday';
    $wednesday   = 'Wednesday';
    $thursday   = 'Thursday';
    $friday   = 'Friday';
    $saturday   = 'Saturday';
    $sundaytest = strpos($job_schedule, $sunday);
    $mondaytest = strpos($job_schedule, $monday);
    $tuesdaytest = strpos($job_schedule, $tuesday);
    $wednesdaytest = strpos($job_schedule, $wednesday);
    $thursdaytest = strpos($job_schedule, $thursday);
    $fridaytest = strpos($job_schedule, $friday);
    $saturdaytest = strpos($job_schedule, $saturday);
    
    if($sundaytest === false){
    ?>S <?
    } else {
    ?><span class="orangeglow">S </span><?
    }
    
    if($mondaytest === false){
    ?>M <?
    } else {
    ?><span class="orangeglow">M </span><?
    }
    
    if($tuesdaytest === false){
    ?>T <?
    } else {
    ?><span class="orangeglow">T </span><?
    }
    
    if($wednesdaytest === false){
    ?>W <?
    } else {
    ?><span class="orangeglow">W </span><?
    }
    
    if($thursdaytest === false){
    ?>T <?
    } else {
    ?><span class="orangeglow">T </span><?
    }
    
    if($fridaytest === false){
    ?>F <?
    } else {
    ?><span class="orangeglow">F </span><?
    }
    
    if($saturdaytest === false){
    ?>S <?
    } else {
    ?><span class="orangeglow">S </span><?
    }
    
    ?>
  • #2 / Oct 20, 2011 10:50am

    Mark Bowen

    12637 posts

    Hi Brian,

    Which version of ExpressionEngine is it that you are running here?

    In the latest version of 2.3.1 the output would be Sunday, Monday, Tuesday etc…

    Are you using the built-in Checkboxes custom field or perhaps a 3rd party add-on here?

    If the built-in Checkboxes field then check out the documentation on that sort of field here in our documentation as that should show you how to get at multiple selections.

    If a 3rd party add-on then you would need to contact the author of the add-on to see how to use it as we can’t support them here I’m afraid.

    Does that help at all?

    Thanks,

    Mark

  • #3 / Oct 20, 2011 11:31am

    briankalwat

    50 posts

    Mark, you’re right - I had the output formatting wrong. (I’m getting what you suggested.) Regardless, I didn’t think there was a way to natively within EE’s capabilities check if a custom field “contains” a value/string - only to check if a value was ==, !=, etc…

    Is there a way around this w/o using PHP?

    *Update* I’m on 2.3.1

  • #4 / Oct 20, 2011 12:03pm

    Mark Bowen

    12637 posts

    Hi Brian,

    I believe this sort of thing should work for you here :

    {job_schedule}
    
    {if item == "Monday"}
    Monday was selected!
    {/if}
    
    {item}
    
    {/job_schedule}

    See if that works for you maybe?

    Thanks,

    Mark

     

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases