0 votes
in SoSci Survey (English) by s267530 (110 points)

Hello,

I have a question A001 in which the respondent chooses an item from a drop down menu. For example, the respondent chooses 'apple' in this question.

Then I want to check if the answer is in a certain list of items:

$fruits = array('apple','pear','banana');

So I am trying to execute this via the following:

replace('%food%', 'A001', 'response');
if (in_array('%food%', $fruits, true))
  {
    $food_type ='is a fruit';
  }
  else
  {
     $food_type ="is NOT a fruit";
}

And then I want to display the outcome to the respondent like so:

html('<p>Your choice '.$food_type.' </p>');

However, when I run this code, it never finds my response 'apple' to be in the array. It always says that the response is not in the array and therefore chooses the option "is NOT a fruit"

Am I doing something wrong here?

I appreciate any advice!

1 Answer

0 votes
by SoSci Survey (325k points)

Placeholders will only work in the output. Instead use value(), please:

if (in_array(value('AB01_01'), $fruits))

Also keen in mind, that value() will return a code (1, 2, 3, ...) unless you use the label parameter. So, a likely code for a selection may look like this:

if (in_array(value('AB01'), [2,4,6,8]))

Willkommen im Online-Support von SoSci Survey.

Hier bekommen Sie schnelle und fundierte Antworten von anderen Projektleitern und direkt von SoSci Survey.

→ Eine Frage stellen


Welcome to the SoSci Survey online support.

Simply ask a question to quickly get answers from other professionals, and directly from SoSci Survey.

→ Ask a Question

...