Problem with Checkbox custom attribute

Permalink
Hi,

I have a checkbox custom page attribute that I am getting in my pagelist+ template like so:
$show_view_button = $page->getAttribute('show_view_button');


I was testing to see if it is checked like this:
<?php  if ($show_view_button == "true"): ?>
... Show button ...
<?php  endif; ?>


But that was not showing the button in my code at all whether ticked or not.
So I changed it to:
<?php  if (isset($show_view_button)): ?>
... Show button ...
<?php  endif; ?>


Now it always shows the button in my code no matter whether it is checked or not.

To complicate matters, I set the attribute to be checked by default, but that doesn't seem to work.

Any ideas please? :)

Thanks
Dave

madesimplemedia
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi madesimplemedia,

Using var_dump(), a checked checkbox attribute is string(1) "1" and an unchecked checkbox attribute is string(1) "0".

This allows you to test for checked or unchecked based on the value of the variable.
Example:
$c = Page::getCurrentPage();
$testCheckbox = $c->getAttribute('test_checkbox');
// checked - string(1) "1"
// unchecked - string(1) "0"
if ($testCheckbox) {
    echo 'checked';
} else {
    echo 'not checked';
}

When checking and unchecking the attribute, remember to refresh the page after saving. If you don't, you won't apply the updated value.
madesimplemedia replied on at Permalink Reply
madesimplemedia
Thanks for the info, I'll give that a go :)