Multiple choices and "if"

Hello everyone.

I want to use multiple choice to show, or not, matching content.

When it's not multiple, no worries, but there, in this case, impossible to make it work.

Thank you for your help

Hello all : {formmenu: apple; banana; cherry; name=choices; multiple=yes}.

{if: choices = "apple"; trim=right}show apple.{endif: trim=left}

{if: choices = "banana"; trim=right}show banana.{endif: trim=left}

{if: choices = "banana"; trim=right}show cherry.{endif: trim=left}

Hi @Frenchy_Devs

Good news, this is possible!

When you have a menu that accepts multiple selections, the selections get saved as a list, which is formatted like this: ["first selection", "second selection", "third selection"] etc...

As a result, you can't do {if: choices="first selection"} but rather need to use the includes() function.

Your snippet would look like:

Hello all : {formmenu: apple; banana; cherry; name=choices; multiple=yes}.

{if: includes(choices, "apple"); trim=right}show apple.{endif: trim=left}

{if: includes(choices, "banana"); trim=right}show banana.{endif: trim=left}

{if: includes(choices, "cherry"); trim=right}show cherry.{endif: trim=left}

1 Like

.... i say again... impossible to find alone this solution :smile:

Big big big thanks for your help @Andrew_Hall and fantastic community !

I use the "contains" formula to accomplish this. What's the difference with the "contains" and "includes" formulas?

Hi Jennifer, the "contains" function is used to check if a string contains a specific substring. For example, if we have the string "apple is good", we can use the "contains" function to check if it contains the substring "apple". On the other hand, the "includes" function is used to check if a list includes a specific element. For instance, if we have a list ["apple", "orange"], we can use the "includes" function to check if it includes the element "apple".

Thank you for the clarification. I see the difference and the benefits of each function. This is very good to know.

1 Like

if i want a string of text display if any of 1 of 4 in a list of 10 do i have rto put includes(listname, " item"4 times?

Hi @F540,

You can use this snippet instead of using includes four times.

{given_list=["item 1", "item B", "item 3", "item 4", "item 5", "item 6", "item 7", "item 8", "item 9", "item 10"]}{if: count(filter(["item A", "item B", "item C", "item D"], (x) -> includes(given_list, x)))>0}Your list contains any of item A, item B, item C, or item D{endif}

The filter function, along with the lambda (x)->includes(given_list, x), is used to only select elements that are present in the given_list. We then check the count, which is the length of the list, to see if it is greater than 0. If there are items such as ["item A", "item B", "item C", "item D"] present in the given list, then the count will be greater than 0. For further information, you can refer to this documentation link.