Skip Navigation

[Resolved] confused about the output of an array

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

Problem:

Check value of custom checkboxes field.

Solution:

You can use Types shortcode with attribute "option" to check each option is checked, for example:

https://toolset.com/forums/topic/confused-about-the-output-of-an-array/#post-1534899

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

This support ticket is created 4 years, 2 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by kenB-4 4 years, 2 months ago.

Assisted by: Luo Yang.

Author
Posts
#1531073

I'm trying to use conditional statements to display an image based on data in a check-box field.

I'm finding the output of code to be inconsistent. For example:
[types field='sun' separator=', '][/types] results in a full array, with this data: full-sun, partial-shade

However, $(wpcf-sun) only returns the first value in the array: full-sun

When I try this code:
(CONTAINS($(wpcf-sun),'partial-shade'))

I get this output:
####################
wpv-conditional attributes
####################
Array
(
[if] => ( ( $(wpcf-sun) = 'partial-shade' ) )
[debug] => true
)

####################
wpv-conditional attributes
####################
Array
(
[if] => (CONTAINS($(wpcf-sun),'partial-shade'))
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: (CONTAINS($(wpcf-sun),'partial-shade'))
--------------------
--------------------
Converted expression: (CONTAINS('full-sun','partial-shade'))
--------------------

When I try this code:
(CONTAINS($('[types field='sun' separator=', '][/types]'),'partial-shade'))

I get this output:
####################
wpv-conditional attributes
####################
Array
(
[if] => (CONTAINS($('full-sun, partial-shade'),'partial-shade'))
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: (CONTAINS($('full-sun, partial-shade'),'partial-shade'))
--------------------
--------------------
Converted expression: (CONTAINS('','partial-shade'))
--------------------

#1531541

Hello,

I assume we are talking about a multiple instance custom field.
The "CONTAINS" comparison works for taxonomy, see our document:
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/displaying-taxonomies-conditionally/#specific

For multiple instance custom field, please try wpv-for-each shortcode, like this:

[wpv-for-each field="wpcf-sun"]
[wpv-conditional if="( '[types field='sun'][/types]' eq 'partial-shade')"]
Here display something
[/wpv-conditional]
[/wpv-for-each]

More help:
https://toolset.com/documentation/user-guides/custom-content/repeating-fields/#Displaying%20Repeating%20Fields

#1532385

OK, I'll try this. I'm using toolset blocks, but I think I can still do this with the fields & text element?

Also, the explanation on CONTAINS is a bit ambiguous in the last paragraph of this document:
https://toolset.com/documentation/user-guides/front-end-forms/cred-conditional-display-engine/

"Checkbox and Select box type fields (and also taxonomy fields) are generally multi-valued fields, which means that the value of a field of this type (when used in an expression) is actually an ARRAY instead of a single value. As such only operators “eq” and “ne” can be used (corresponding to “=” equal and “<>” not-equal) meaning either this value is contained in the values array or not. "

This implies that you can simply use eq instead of contains but that it will function the same as contains?

#1532653

By the way, this doesn't work. Debug output produces:

####################
wpv-conditional attributes
####################
Array
(
[if] => ( 'full-sun, partial-shade' = 'partial-shade')
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( 'full-sun, partial-shade' = 'partial-shade')
--------------------
After replacing 1 general variables and comparing strings: ( 'full-sun, partial-shade' = 'partial-shade')
Comparing full-sun, partial-shade to partial-shade

So, the notion that using the 'eq' to find a value contained in a multi-value field doesn't seem to work. Trying the CONTAINS method now, but not optimistic because it sounds like this function doesn't work anywhere except in taxonomies?

#1532657

update:
Contains method appears to work, but I'm not getting any of the content that is supposed to be displayed when condition is met.

Here's the code I'm using within the Fields & Text element in block editor:

[wpv-for-each field="wpcf-sun" debug="true"]
  [wpv-conditional if="( contains('[types field='sun'][/types]', 'partial-shade'))" debug="true"]
 <p>here is the icon:<br/> <img class="alignnone" src="<em><u>hidden link</u></em>" alt="partial shade" width="50" height="50" /></p>
  [/wpv-conditional]
  [/wpv-for-each]

debug output shows this:
####################
wpv-conditional attributes
####################
Array
(
[if] => ( contains('full-sun, partial-shade', 'partial-shade'))
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( contains('full-sun, partial-shade', 'partial-shade'))
--------------------

So, the syntax looks like it should be good and the condition should result in true. So, why isn't it displaying my content?

#1534899

Thanks for the details, I assume we are talking about a custom checkboxes field "sun" with two options:
- full-sun
- partial-shade

You can use Types shortcode with attribute "option" to check each option is checked, for example:

[types field="sun" option="0" state="checked"] option full-sun is checked[/types]
[types field="sun" option="1" state="checked"] option partial-shade is checked[/types]

Similar as above, with [wpv-conditional] shortcode, you can try these:

[wpv-conditional if="( '[types field='sun' option="0"][/types]' eq 'full-sun')"]
option full-sun is checked
 [/wpv-conditional]

[wpv-conditional if="( '[types field='sun' option="1"][/types]' eq 'partial-shade')"]
option partial-shade is checked
 [/wpv-conditional]

See our document:
https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes
option: "zero-based index number"
e.g. option=”0″ Will output the value for the option number specified.

#1542431

Thanks, that worked. I'm not sure why I wasn't able to find the right documentation for handling this. I must not have understood how the checkbox field worked, as I would never have known to include the "option=0" portion. This wasn't intuitive, as the value stored in the database is not 0, 1, 2... instead the value stored is 'full-sun', 'partial-shade', etc.

Thanks for your help; I have a number of fields that are using the checkbox type, so this will help me with all of the others as well.

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