Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Random Image won't load

  • Burney
  • Burney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 6 months ago #140993 by Burney
Random Image won't load was created by Burney
Hi,
in my questionnaire I have an equation-type question "Valence" containing {rand(1,2)}.

In a subsequent question, I want to load one of three random images associated to this condition:
Code:
<img alt="" src="/Mediennutzung/Limesurvey2/upload/surveys/937241/images/{rand(1,3)}{if (Valence == 1, 'pos', 'neg')}.jpg" style="width: 800px;" />

However, when I test the question group, I just get "2<span id='LEMtailor_Q_33821_52'>neg</span>.jpg" as a response and the browser can't load the image. I then placed the equation in a group before the one containing the picture. This time an image was displayed, but completely unrelated to the condition from the valence-variable (e.g. "1" resulted both in "pos" and in "neg").

Am I doing something wrong?

Thank you for your help!
Benny
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #140995 by Joffm
Replied by Joffm on topic Random Image won't load
Hi, Benny
if I understand your condition right, you have 6 pictures.
1. the rand(1,3)
2. the valence that is rand(1,2)
So you want to get
1pos.jpg, 1neg.jpg, 2pos.jpg, 2neg.jpg, 3pos.jpg and 3neg.jpg

Well, I do not see any issue when I include the following in the question text.
<img alt="" src=" www.myserver.de/Holiday/Boat00{rand(1,4)}_small.jpg " />
It is not exactly your condition, but it shows that the random function works.

Have alook into the source code of the question (the span-tag looks suspicious)

Best regards
Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.
  • Burney
  • Burney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 6 months ago #140998 by Burney
Replied by Burney on topic Random Image won't load
Actually, the rand(1,3) function works for me as well – it is always the picture with the generated number in its name. The problem is the second condition pos/neg as the question simply doesn't seem to care what number the previous equation-type question rolled. The span-issue has also been reported in other threads (e.g. here ). The span is not part of the source code, but is generated when the question is loaded in the browser.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #141000 by tpartner
Replied by tpartner on topic Random Image won't load
Try placing something like this in an equation type question (let's call it "equation1"):
Code:
{htmlentities('<img alt="" src="/Mediennutzung/Limesurvey2/upload/surveys/937241/images/'+rand(1,3)+''+if(Valence == 1, 'pos', 'neg')+'.jpg" style="width: 800px;" />')}

Then you should be able to insert the image where required like this:
Code:
{html_entity_decode(equation1)}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Burney
  • Burney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 6 months ago #141016 by Burney
Replied by Burney on topic Random Image won't load
Thank you for the suggestion, Tony! I created equation1 as instructed an inserted the "{html_entity_decode(equation1)}" tag in the following question. This time, the image loads correctly without generating the span, but the "pos" or "neg" attribute still appears to be completely unrelated to the content of the variable "Valence" being 1 or 2. That is, for 1 I get both "pos" and "neg" images and vice versa. :blink:

Do you perhaps have any idea what's going wrong here?
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #141017 by tpartner
Replied by tpartner on topic Random Image won't load
If "Valence" is conditional, you may need to use this IF statement:
Code:
if(Valence.NAOK == 1, 'pos', 'neg')

Other than that, all I can suggest is placing that IF statement in a separate equation type question to try and debug it.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Burney
  • Burney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 6 months ago #141020 by Burney
Replied by Burney on topic Random Image won't load
Thanks again. Valence is always computed so that the .NAOK-statement should not be necessary, but I tried it nonetheless. Placing the if-statement in an external equation (and having the value show up in the survey for debugging) yields the same result: The "pos" or "neg" is unrelated to the content of the Valence variable ... I am really not sure what to debug here as I am absolutely sure that (this very simple) if-statement is correct.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #141023 by tpartner
Replied by tpartner on topic Random Image won't load
Can you attach a small test survey containing only the related questions?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • Burney
  • Burney's Avatar Topic Author
  • Offline
  • New Member
  • New Member
More
7 years 6 months ago #141024 by Burney
Replied by Burney on topic Random Image won't load
Here's the survey - it's still very small as I am still trying to figure out the randomization.

Thank you in advance!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
7 years 6 months ago #141029 by tpartner
Replied by tpartner on topic Random Image won't load
It looks like the equation in Valence may be fired more than once. To ensure that a random number is only generated once, use this in Valence:

Code:
{if(is_empty(Valence), rand(1,2), Valence)}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose