If/Else Statement for Custom Checkbox
-
I set up a multi-select field in my content type “Product”. With this, the admin user could use checkboxes to indicate which options are available for a particular product. Then, with an additional code for a cluetip, someone could mouseover the name of the options in the front-end, and have a cluetip pop up which has further information about that option (what it costs extra, what kinds of modifications are done from the original product to achieve this option, etc…) However, I want to make sure that each of these options (output filter is set to “array” right now) only displays if the administrator has checked the box. If not, the below code should just echo “nope” for every unchecked option.
<strong> Option Array: </strong> <?php $my_arr = get_custom_field('product_options'); $opts = array( 'Option 1' => '<div>Option 1 description</div>', 'Option 2' => '<div>Option 2 description</div>', 'Option 3' => '<div>Option 3 description</div>', ); foreach($my_arr as $val) { if (in_array($val, array_keys($opts))) echo $val . "<br/>"; else echo 'nope<br/>'; } ?>
Can this be done with said conditionals? Currently my efforts to have the word “nope” echoed in place of an option (in that array) which is not toggled on in the backend, has not been successful.
How would it look if neither of the 3 options were checked. Currently, with the above code, if I have a product content type with no additional optiosn checked on by an admin, it just returns the error:
Warning: Invalid argument supplied for foreach()
For those who would want to suggest I ask this on a PHP forum (since it does deal with if/else statements), the current topic is going on in this Stack Overflow question:
https://stackoverflow.com/questions/6741370/in-array-based-if-then-else-statement
- The topic ‘If/Else Statement for Custom Checkbox’ is closed to new replies.