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

Check-box array

Valhalla

Well-known member
#1
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
 

Chris D

XenForo developer
Staff member
#2
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

Well-known member
#4
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>
 

Chris D

XenForo developer
Staff member
#5
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?