1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Check-box array

Discussion in 'XenForo Development Discussions' started by Valhalla, May 26, 2014.

  1. Valhalla

    Valhalla Well-Known Member

    I'm creating a check-box option. How could I check within templates whether my array is populated with at least one non-false value?

    Code:
    array(3) {
    ["option_1"] => string(1) "1"
    ["option_2"] => string(1) "1"
    ["option_3"] => bool(false)
    }
    For example, if my setting array returned the following, I would want my condition not to be true:

    Code:
    array(3) {
    ["option_1"] => bool(false)
    ["option_2"] => bool(false)
    ["option_3] => bool(false)
    }
    Screen Shot 2014-05-26 at 23.26.57.png
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Assuming the option_id is checkBoxes:

    PHP:
    $condition true;
    foreach (
    $option->checkBoxes AS $checkBox)
    {
        if (
    $checkBox === false)
        {
            
    $condition false;
        }
    }

    Zend_Debug::dump($condition); // If any of the check boxes are false, $condition == false. If none of the checkboxes are false, $condition == true.
    EDIT: Just saw you wanted to check it in templates... You could probably fashion a similar thing in template syntax.
     
    Valhalla likes this.
  3. Valhalla

    Valhalla Well-Known Member

    That works for me, thanks.
     
  4. Valhalla

    Valhalla Well-Known Member

    I've decided to try and get this working within templates. I tried to use the conditional below, but I'm getting a syntax error so it won't save, and I'm not sure why.

    My option_id is checkBoxes.
    Code:
    <xen:if is="in_array(true, array_keys({$xenOptions.checkBoxes}))">
               (At least one check-box is selected)
    </xen:if>
    EDIT: I'm not checking the keys, only the values, so I've worked it out using:

    Code:
    <xen:if is="in_array(true, {$xenOptions.checkBoxes}">
               (At least one check-box is selected)
    </xen:if>
     
  5. Chris D

    Chris D XenForo Developer Staff Member

    in_array is allowed in templates, but I don't think array_keys is. And array_keys would be incorrect any way. The array_keys from your example would be one, two, three.

    If you removed array_keys and just did:

    Code:
    <xen:if is="in_array(true, {$xenOptions.checkBoxes})">
        (At least one check-box is selected)
    </xen:if>
    Does that work?
     
    Valhalla likes this.

Share This Page