Skip Navigation

[Resolved] Conditional Output for Checkboxes

This thread is resolved. Here is a description of the problem and solution.

Problem:
I have a Checkboxes Field and want to output content conditionally depending on that Field's checked options, using HTML conditionals.
What's the syntax?

Solution:
Checkboxes are complex fields, that either store an empty array in the database if not checked and set to save nothing or an array with the value chosen.
Note that save 0 for unchecked is set, then it'll instead of saving empty array save a 0 when not checked.
We do not recommend to use "When unchecked, save "0" to the database.".

In Toolset HTML Conditionals this, however, should not be of concern.

Your HTML Condition to output "XY" when no option is checked and the options themselves if checked would look like this:

[wpv-conditional if="( $(wpcf-your_field_slug) eq '0' )"]test[/wpv-conditional]

Above will only show if no option is checked at all (Note, this example uses the *Save 0 for unchecked*. Otherwise, you'd compare against "empty" such as

[wpv-conditional if="( $(wpcf-your_field_slug) eq '')"]test[/wpv-conditional]

).

Relevant Documentation:
https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/

This support ticket is created 4 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 3 replies, has 2 voices.

Last updated by Engedi 4 years, 9 months ago.

Assisted by: Beda.

Author
Posts
#1263031

I am trying to edit a content template for "Forex Broker"

Before this I have already created a Post Field Group for forex broker. One of the field is "Country of Origin".

"Countries of Origin" is a checkboxes field. I have added 199 countries to it. Below is the exact setting:
When unchecked, save "0" to the database.
title: countrie name
value to store: 1-199 (different for every country)

show one of these 2 values:
not selected: empty
selected: country name

Now in the content template. What i want to do is that, if none of the checkboxes are checked, It will show a message:
Country of Origin not found.

If any of the checkboxes are checked, then it will show:
Countries of Origin: Country 1, country 2, country 3.

I try to use output conditional. But the result is weird. It does not work as it should.

example:
Content Template:

<!--If country-of-origin is set to zero (no countries selected)--> 
  [wpv-conditional if="( $(wpcf-country-of-origin) eq '0' )"] <p>No Country of Origin Found</p>
[/wpv-conditional]

<!--If country-of-origin is set to zero (some countries are selected)/--> 
[wpv-conditional if="( $(wpcf-country-of-origin) ne '0' )"]
<p>Country of Origin: [types field='country-of-origin' separator=', '][/types] </p>
[/wpv-conditional]

Then I ticked 3 checkboxes.
Result:

No Country of Origin Found.

If i put following code in the content template:


<!--If country-of-origin is set to blank (no countries selected)--> 
  [wpv-conditional if="( $(wpcf-country-of-origin) eq '' )"] <p>No Country of Origin Found</p>
[/wpv-conditional]

<!--If country-of-origin is set to blank (some countries are selected)/--> 
[wpv-conditional if="( $(wpcf-country-of-origin) ne '' )"]
<p>Country of Origin: [types field='country-of-origin' separator=', '][/types] </p>
[/wpv-conditional]

Then I ticked 3 checkeboxes. Result is:

Country of Origin: Country 1, Country 2, Country 3.

But when there is no checkboxes checked. The result:

Country of Origin:

................................

I am super confused of how all the values are stored in the database. Can you please provide me the exact code of how to display what I want?

THank you very much!!

#1263069

Checkboxes are complex fields, that either store an empty array in the database if not checked and set to save nothing or an array with the value chosen.
If in the Fields settings it was chosen to save 0 for unchecked then it'll instead of saving empty array save a 0 when not checked.
We do not recommend to use "When unchecked, save "0" to the database.", please read more about this here
https://toolset.com/errata/checkboxes-that-save-0-are-interpreted-as-checked-by-views/

In Toolset HTML Conditionals this, however, should not be of concern.
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/checking-types-fields-and-custom-fields/

Your HTML Condition to output "XY" when no option is checked and the options themselves if checked would look like this:

[wpv-conditional if="( $(wpcf-your_field_slug) eq '0' )"]test[/wpv-conditional]

Above will only show if no option is checked at all (Note, I used the *Save 0 for unchecked* as you did. Otherwise, you'd compare against "empty" such as [wpv-conditional if="( $(wpcf-your_field_slug) eq '')"]test[/wpv-conditional]).
The options themselves can simply be displayed by the field shortcode which will return nothing if empty.
If there are options checked they are output, if not, nothing is output.

The above code was tested locally and needs edit in the slugs of the fields

What is a concern theoretically are 100eds of options in the Checkboxes field, that might use a lot of resources, specially in a Views loop.

#1263223

Thank you for your help and explanation.

I have set following option in the checkboxes group field.

When unchecked, don't save anything to the database

Then in the content template following code:

<!--If country-of-origin is set to blank (no countries selected)--> 
  [wpv-conditional if="( $(wpcf-country-of-origin) eq '' )"] <p>No Country of Origin Found</p>
[/wpv-conditional]
 
<!--If country-of-origin is set to blank (some countries are selected)/--> 
[wpv-conditional if="( $(wpcf-country-of-origin) ne '' )"]
<p>Country of Origin: [types field='country-of-origin' separator=', '][/types] </p>
[/wpv-conditional]

It works then perfectly fine..

#1263225

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.