Inhaltsverzeichnis

loopToPage()

int loopToPage(string pageID, int repetitions)

The function loopToPage() ensures that the pages from the current page to the page with the ID pageID are displayed multiple times, as often as specified in repetitions.

The function returns how often the pages have already been repeated. So, 0 in the first round, 1 in the second round, etc.

Important: The function loopToPage() cannot be used simultaneously with loopPage(), setNextPage() or setPageOrder().

Note: Use the function loopIndex() to determine within the repetitions which repetition is currently running.

Note: The function loopToPage() allows repeating similar “question blocks”, but the questions must be created in the list of questions as often as needed. If several questions shall be repeated multiple times, a Multi-Level Structure may be the better solution.

Example: Repeat Pages with Different Questions

You want to repeat pages 5 to 8 of the questionnaire 3 times in total. Different questions shall be displayed each time.

loopToPage('loopend', 3);

Now you want the questions A001 to A005 to be displayed in the first round. In the second round, the questions A101 to A105 shall be displayed, and in the third round the questions A201 to A205 (the questions were created in section A0, and then the section was copied twice by downloading it as a file and importing it again).

Place the following PHP code on page 5. It creates an array that contains the list of questions to be shown, and makes the array available for the following pages via registerVariable().

$questions = array(
  0 => array('A001', 'A002', 'A003', 'A004', 'A005'),
  1 => array('A101', 'A102', 'A103', 'A104', 'A105'),
  2 => array('A201', 'A202', 'A203', 'A204', 'A205')
);
registerVariable($questions);

To display the first two questions on the first page (no. 5), extend the code of loopToPage() as follows:

$i = loopToPage('loopend', 3);
// Now retrieve the part of $questions that is relevant for the current round
$set = $questions[$i];
// Show the first two questions of it
question($set[0]);
question($set[1]);

To show the next question on page 6, place the following PHP code on page 6. The command loopIndex() tells in which round the page is displayed. The remaining code is equivalent to the code above.

$i = loopIndex();
$set = $questions[$i];
// Show the next question (index 2)
question($set[2]);

Repeat the code above on pages 7 and 8. Replace the 2 in the last line with 3 or 4, respectively.

Example: Variable Number of Repetitions

As in example 1, you want to repeat pages 5 to 8 and have given page 8 the ID “loopend”. Beforehand, on page 3, you asked for up to 10 brand names with a question of the type “free mentions” (ON01) – and the pages shall now be repeated for each mention of the participant.

In the list of questions, you have copied the section A0 with the corresponding questions 9 times (sections A1 to A9). Accordingly, the questions on page 5 are first defined as follows (see example 1):

$questions = array(
  0 => array('A001', 'A002', 'A003', 'A004', 'A005'),
  1 => array('A101', 'A102', 'A103', 'A104', 'A105'),
  2 => array('A201', 'A202', 'A203', 'A204', 'A205'),
  3 => array('A301', 'A302', 'A303', 'A304', 'A305'),
  4 => array('A401', 'A402', 'A403', 'A404', 'A405'),
  5 => array('A501', 'A502', 'A503', 'A504', 'A505'),
  6 => array('A601', 'A602', 'A603', 'A604', 'A605'),
  7 => array('A701', 'A702', 'A703', 'A704', 'A705'),
  8 => array('A801', 'A802', 'A803', 'A804', 'A805'),
  9 => array('A901', 'A902', 'A903', 'A904', 'A905')
);
registerVariable($questions);

The same result can be achieved more elegantly with a FOR loop. Alternatively, you can therefore use the following PHP code:

$questions = array();
// Create all 10 repetitions (index 0 to 9)
for ($i=0; $i<=9; $i++) {
  $section = 'A'.$i;  // A0 to A9
  $questions[$i] = array();
  // Write the questions 01 to 05 into the respective sub-list
  for ($f=1; $f<=5; $f++) {
    $questions[$i][$f-1] = $section.sprintf('%02d', $f);
  }
}
registerVariable($questions);

Now, on page 5, it must be determined which fields of the “free mentions” the participant has filled out at all. This list is made available for the following pages with registerVariable(). At the same time, the length of the list is determined with count(), because it corresponds to the number of repetitions. loopToPage() then prepares the repetition of the pages.

$items = getItems('ON01', 'valid');
registerVariable($items);
$i = loopToPage('loopend', count($items));

Now the correct set (i.e. the sub-list) from the list $questions must be determined from $items and the number of the repetition. From this sub-list, e.g. the first two questions can then be displayed on page 5:

$id = $items[$i];  // $i was determined by loopToPage(), see above
$set = $questions[$id];
// As in example 1
question($set[0]);
question($set[1]);

Here is the complete PHP code for page 5 once again:

$questions = array(
  0 => array('A001', 'A002', 'A003', 'A004', 'A005'),
  1 => array('A101', 'A102', 'A103', 'A104', 'A105'),
  2 => array('A201', 'A202', 'A203', 'A204', 'A205'),
  3 => array('A301', 'A302', 'A303', 'A304', 'A305'),
  4 => array('A401', 'A402', 'A403', 'A404', 'A405'),
  5 => array('A501', 'A502', 'A503', 'A504', 'A505'),
  6 => array('A601', 'A602', 'A603', 'A604', 'A605'),
  7 => array('A701', 'A702', 'A703', 'A704', 'A705'),
  8 => array('A801', 'A802', 'A803', 'A804', 'A805'),
  9 => array('A901', 'A902', 'A903', 'A904', 'A905')
);
registerVariable($questions);
 
$items = getItems('ON01', 'valid');
registerVariable($items);
$i = loopToPage('loopend', count($items));
 
$id = $items[$i];
$set = $questions[$id];
question($set[0]);
question($set[1]);

On pages 6 to 8, only the lower part is repeated with slight modifications:

$i = loopIndex();
$id = $items[$i];
$set = $questions[$id];
question($set[2]);  // Index 2 shows question 3, because the index counting starts at 0

Example: Repeat Question Blocks

A block of 5 questions (across 3 pages “blockStart” to “blockEnd”) shall be repeated for each sibling of a participant. Afterwards, the questionnaire continues with the page “demographics”.

Note: This example is comparable to the first one, but assumes that you have a similar section in the questionnaire for each repetition (in the first example, the questions of a repetition can come from different sections or all from the same one).

As preparation, the 5 questions were created in section “G1” and the section was copied 4 times (“G2” to “G5”) to cover cases of up to 5 siblings. In addition, question GS01 asked how many siblings a person has (1=one sibling, 5=five or more siblings, -1=no siblings).

On the first page of the “question block” (“blockStart”), the number of siblings is determined first and loopToPage() is called accordingly.

$count = (int)value('GS01');
// Filter: No siblings
if ($count < 0) {
  goToPage('demographics');
}
// Repeat question block
loopToPage('blockEnd', $count);
// Definition of the sections
$sections = array('G1', 'G2', 'G3', 'G4', 'G5');
registerVariable($sections);

On the first page, the questions 01 and 02 from the respective section are now displayed.

$i = loopIndex();
$section = $sections[$i];
 
question(id($section, 1));  // Asks G101, G201, G301, ...
question(id($section, 2));  // Asks G102, G202, G302, ...

Similar code can be found on page 2 of the block, which shows question 03 from the section.

$i = loopIndex();
$section = $sections[$i];
 
question(id($section, 3));  // Asks G103, G203, G303, ...

And on the third and last page, we write it a little more compactly for questions 04 and 05:

$section = $sections[loopIndex()];
 
question(id($section, 4));
question(id($section, 5));