0 votes
in SoSci Survey (English) by s310769 (110 points)
reopened by SoSci Survey

Hi,
I have a coding-related doubt regarding insert images. I want to ask questions on different images appearing randomly on every subsequent page. I have uploaded all the images but cannot store them as an array variable and then access them in php code. Please help regarding this.

Thank you

by s109993 (12.2k points)
Have you checked the manual?
https://www.soscisurvey.de/help/doku.php/en:create:randomization-media

If you have a problem with your code, it would be better if you post the code you are using, otherwise we can not really help you :)
by SoSci Survey (335k points)
Please also describe how your pages are organized, and what they show. Regarding an efficient solution, it's an important difference whether you only show images or also text or questions and how these are organsized.
by s310769 (110 points)
Hi, this is the code I'm using for a template survey page. What I want to do is to introduce a loop of images where a different image appears on a new survey page without repetition and the same questions are being asked for every page, however the order of varaibles in the questionnaire should be randomized

html('
<div class="spacing" style="display: flex; justify-content: flex-start; align-items: center; height: 100vh; padding: 0 50px; gap: 50px; margin-top: -40px">
   <img src="AD2.png" style="width: 25; height: 400px; margin-top: -90px"> // Here I want to introduce the image loop. as there are 120 images, I dont want to manually create 120 pages.
    <!-- Question Section -->
    <div style="flex-grow: 1; text-align: left; padding-left: 20px;"; margin-top: -170px>
');

question('FP02');
question('FP03');


html('
    </div>
</div>
');

option("nextbutton", 'Next');

Please get back asap. Thank you very much for your help.

1 Answer

0 votes
by SoSci Survey (335k points)

Hi, this is the code I'm using for a template survey page.

I strongly recommend to put the HTML code into the questionnaire layout, if you need that for multiple pages. Usually, it's even better to put it into a separate layout and select the via option('layout', ...), if you need the modified layout for just a single question.

What I want to do is to introduce a loop of images

Loops in SoSci Survey are implemented via loopPage() or loopToPage(), and the rotation of the contents is usually done with a random generator. There are two different approaches:

You can either rotate pages (without the loop), which is useful if you want to present the images along with a question, each: Rotate Pages

Our you can rotate just the content on a single page, and use loopPage() to have the respondents see multiple pages. By and large it works like explained in Rotate Questions, the difference is to put the image names into the random generator and put the result into a piece of HTML code.

// Get a list of the image names
show('RG01'); 
// Get a list of the image names
$images = array_values(valueList('RG01', null, 'label'));
// Do the loop
$image = loopPage($items); 
// Show the image
html('<div><img src="'.$image.'" alt=""></div>');
by s310769 (110 points)
Thank you for your quick response. I put in another code and I can see that its working. i have another issue, how do i put the slider back to 0 after pressing next. the next screen is carrying the previous values. Also, would this code make sure that it displays every image stimulus. I will check it manually once but just wanted to run it by you in terms of correct usage of functions. Thank you for your help :)

// List of all available images
$available_images = array('S1.jpg', 'A1.jpg', 'SD1.png', 'AD1.png', '501.png', 'N1.jpg', 'S2.jpg', 'A2.png', 'SD2.png');

// Create a variable to keep track of used images
if (!isset($used_images)) {
    $used_images = array();  // Initialize used images array
}

// Loop through 11 pages
loopToPage('04', 11);

// Ensure image selection is unique for each page
$filename = '';
do {
    // Generate a random number to select an image
    $code = random(1, count($available_images));

    // Assign the image filename based on the random number
    $filename = $available_images[$code - 1];  // Array index starts at 0

} while (in_array($filename, $used_images));  // Check if the image has already been used

// Add the selected image to the used images array
$used_images[] = $filename;

// HTML for layout with dynamic image
html('
<div class="spacing" style="display: flex; justify-content: flex-start; align-items: center; height: 100vh; padding: 0 50px; gap: 50px; margin-top: -40px">
   <img src="' . $filename . '" style="width: 25%; height: 400px; margin-top: -200px">
    <!-- Question Section -->
    <div style="flex-grow: 1; text-align: left; padding-left: 20px; margin-top: -20px;">
');

// Display the fixed questions FP02 and FP03
question('FP02');

question('FP03');

html('
    </div>
</div>
');

// Display the fixed question FP04
question('FP04');

// Option for navigation
option("nextbutton", 'Next');
by SoSci Survey (335k points)
> how do i put the slider back to 0 after pressing next. the next screen is carrying the previous values.

If you want to ask the same question multiple times, you can either (a) mak multiple copies of the question, and show a different one, each time, (b) make a question with multiple items, and only show one of them, each time, or (c) word with a multi-level structure.

Just displaying the same fixed questions over and over again will only work with the multi-level structure: https://www.soscisurvey.de/help/doku.php/en:create:multilevel

Actually, I do not see the point in using the while-loop in your code. It looks a bit like ChatGPT hat some idea - but that does not fit how an online questionnaire works. I strongly recommend to only use code that you understand. We had two projects last year that lost all their data, because the trusted in ChatGPT.
by s310769 (110 points)
Hi, in this code suggested by you:

// Get a list of the image names
show('IR01');
// Get a list of the image names
$images = array_values(valueList('IR01', null, 'label'));
// Do the loop
$image = loopPage($images);
// Show the image
html('<div><img src="'.$image.'" alt=""></div>');

My question here is that the IR01 is the random generator in which all images are labelled. However, the above code is not going on a loop. It displays one random image and moves onto next screen. Should I put a loopPage on top of this let it run for size of the $images array?
Also, regarding copying questions, could you elaborate on it? I dont think multi-level structure will make sense. I have two slider-based questions (facial ratings with 6 and 2 items respectively) for each unique image. Can i create a copy of the variables for each loop and change the variable name (like concatenating the name as variable_<no. of iteration> on every iteration till all the images are displayed? if yes, could you suggest something in that regard? Also, I need to randomize the sequence of the items in one questionnaire which I have figured out with this code.
if (!isset($question)) {
  // Select a fixed set of 6 random items from FP02
        $question = random_items('FP02', 6);

}
loopPage(3);
question('FP02',$question);

The only issue is that the slider doesnt come back to 0. please any help would be appreciated. Thank you
by SoSci Survey (335k points)
>  the above code is not going on a loop.

If you add the following line, what does the debug information say?

debug($images);

How many images did you tell the random generator to draw per interview?

> Also, regarding copying questions, could you elaborate on it?

How many images are we talking about?

> Also, I need to randomize the sequence of the items in one questionnaire which I have figured out with this code.

Do you need the items on separate pages? Otherwise you could simply nable the item rotation in the question.

And ... do you need this question for every image? You cannot mix two loopPage() loops. That would also explain, why ony one image is shown in your case.
by s310769 (110 points)
Hello,
Thank you for your help so far. So, the image code is working now, I had to change number of draws per interview. Earlier it was set to 1.
Also, earlier, I was trying to automate the repeated questions loop and image change loop stimulus on one page. But then, as soon as I change the page with looppage, the responses to the answer will override if I dont manually create copy of those questions (also you said two looppage won't work).

Right now, I have a total of 120 image stimulus and for each stimulus same two questions (with randomized sequence of items) will be answered for which I have provided the code below. However, I am struggling to put these codes together.

For image display:
// Show the question or item 'IR01'
show('IR01'); // here the image codes are stored in random generator

// Retrieve the values (labels) from 'IR01'
$images = array_values(valueList('IR01', null, 'label'));

$image = loopPage($images);
// Show the image
html('<div><img src="'.$image.'" alt=""></div>');

For display of questions:
$q1=random_items('FP04',6);
question('FP04',$q1);  // first questionnaire
$q2=random_items('AA02',2);
question('AA02',$q2); // second questionnaire

I am okay manually creating 120 pages and pasting them on every page (would be happy if there's another way). I don't know if rotatepage will help in that context. Anyway, how can I combine these codes placing them side by side and making sure that a random image without repetition appears on different pages.

Best,
Kartik
by s310769 (110 points)
the problem is once random generator generates image, its only for that page. can I declare the images array and image variable as registervariable (so that they becomes global and will have unique values of image[0], image[1] for different pages?
by SoSci Survey (335k points)
You may want to take a look at this newly written manual, although it's not yet translated to English, but deepl.com should do the job: https://www.soscisurvey.de/help/doku.php/de:create:multilevel#zufallsziehung

Collecting the data for 120 images in one record in the dataset might make data analysis much harder. Using a multi levle structure seems a much better solution for that.

If were were talking abount 5 oder 10 pages, well ... but not for 120. In general I would recommend not to let someone rate 120 images in one session at all. If the mind closes down after 30 or so images, the responses will only be a trash pile of data.

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

...