Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Conditional Image on basis of question num

I have two questions Q1 and Q2. The list of both questions are same but i have to change the image of list members.

List members are:

1. Pepsi
2. Coke
3. Fanta

In Q1, we are showing bottles of pepsi, coke and fanta whereas in Q2, we are showing tin can of pepsi, coke and fanta.

I am making list as:

List member 1:

[% Begin Unverified Perl

my $a=QUESTIONNAME();

if($a=="Q1")
{
return pepsibottle.jpg;
}
else
{
return pepsican.jpg;
}

End Unverified %]

It is not working. It is showing pepsican everytime.

If i return Questionname, then it is working fine.

Any suggestion?
asked Nov 10, 2011 by anonymous
are you sure you're question name is uppercase in the study?
Yes, QUESTIONNAME() works fine. I tested this by returning $a, correct label is coming like Q1, Q2.

But when we use


if($a=="Q1")
{
return pepsibottle.jpg;
}
else
{
return pepsican.jpg;
}

it is always returning pepsibottle. Sorry, i wrote pepsican earlier.

1 Answer

0 votes
 
Best answer
in Perl string comparison is indicated as eq not ==
[% Begin Unverified Perl
my $a=QUESTIONNAME();
if($a eq 'Q1')
{
return 'pepsibottle.jpg';
}
else
{
return 'pepsican.jpg';
}
End Unverified %]


works
answered Nov 10, 2011 by Bahadir Ozkurt Gold (17,010 points)
I've tried the script above, but it doesn't recognize the picture. It shows me a text instead. How can i write the path for the picture?

Thanks!
Are you referencing the picture files correctly?

Save the picture files in the job graphics folder and use the [%GraphicsPath()%] function.

e.g. [%GraphicsPath()%]picture1.jpg
the script above only relates to the image name. the easiest way would be the following:

1- insert a random picture from under graphics folder
2- it would look like
<img source="[GraphicsPath()]random_picture.jpg">

3- edit out the random_picture.jpg part and replace it with the code above
<img source="[GraphicsPath()][% Begin Unverified Perl
my $a=QUESTIONNAME();
if($a eq 'Q1')
{
return 'pepsibottle.jpg';
}
else
{
return 'pepsican.jpg';
}
End Unverified %]">
I wrote the following code (I need a picture for Q13 and a text for the rest of the questions):

[% Begin Unverified Perl
my $a=QUESTIONNAME();
if($a eq 'Q13')
{
return '<img src="[%GraphicsPath()%]Kent HD.jpg" alt="" border="0">';
}
else
{
return 'Kent HD';
}
End Unverified %]


It gives me this error: Begin Unverified Perl without End Unverified

Thanks!
instead of
return '<img src="[%GraphicsPath()%]Kent HD.jpg" alt="" border="0">';

try
return '<img src=\"[%GraphicsPath()%]Kent HD.jpg\" alt=\"\" border=\"0\">';

i've added \ before each ", it is escape character for PERL, telling it to treat the quotation mark as a plain text rather than a string terminator.

let me know if it works
No, it doesn't work. Same error.
ah it doesnt seem to like the [ and ] as well so we need to "escape" these as well
return '<img src=\"\[%GraphicsPath()%\]Kent HD.jpg\" alt=\"\" border=\"0\">';
works
...