Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:create:functions:registervariable [28.12.2014 17:21] – [Example 2] alexander.ritteren:create:functions:registervariable [29.09.2020 17:28] (current) sophia.schauer
Line 7: Line 7:
   * //variableName//\\ A string (in quotation marks) with the name of the variable. This cannot be the variable itself (e.g. ''$item''), a string with the name of the variable (e.g. 'item').   * //variableName//\\ A string (in quotation marks) with the name of the variable. This cannot be the variable itself (e.g. ''$item''), a string with the name of the variable (e.g. 'item').
  
-===== Example 1 =====+===== Use with isset() ===== 
 + 
 +Many examples in the manual use ''registerVariable()'' together with the PHP function ''[[http://php.net/manual/de/function.isset.php|isset()]]'' to avoid multiple [[:en:create:randomization|Randomization]] or [[:en:create:rotation|Rotation]] functions -- for example if the questionnaire page is displayed again after "Next" because answers are still missing. 
 + 
 +The function ''isset()'' determines whether a PHP variable has already been defined before. This way you can avoid that e.g. a list is created and merged if it has been defined before. 
 + 
 +**Note:** The content of the variable is __not__ stored in the dataset and the PHP variable is deleted when the "last page" is reached (completion of the questionnaire).  If the content of the variable (e.g. the order of a rotation) is relevant for the evaluation, you have to save it explicitly in the dataset using ''[[:en:create:functions:put]]''
 + 
 +**Wichtig:** When using ''registerVariable()'' you have to be aware that the registered variable is defined for the whole rest of the interview. If one would like to use the combination ''registerVariable()'' and ''isset()'' again at another place, one should choose a __different name for the variable__. For example ''$itemsAB'' in the first PHP code and ''$itemsBC'' in the second. 
 + 
 +Here is an example where the items of a question are mixed specifically: 
 + 
 +<code php> 
 +// The bracket is only executed, 
 +// if the variable $items has not yet been defined, 
 +// at the first visit of this page - and only then, 
 +// if there is no registerVariable($items) in the questionnaire before. 
 +if (!isset($items)) { 
 +  // Here the items of question AB01 are retrieved and mixed 
 +  $items = getItems('AB01'); 
 +  shuffle($items); 
 +  // And this is where SoSci Survey makes sure that the variable $items 
 +  // does not immediately forget them again, but rather keeps them for the complete 
 +  // remaining interview (including a possible 
 +  // call the current page again). 
 +  registerVariable($items); 
 +
 +// Here the content of the variable is now used 
 +question('AB01', $items); 
 +</code> 
 + 
 +From now on the variable ''$items'' is defined on every further page in the questionnaire in every PHP code. If you would place the same PHP code on another page, then ''isset($items)'' would give the result ''true'', the negation with the exclamation mark ''!isset($items)'' would be ''false'' (not true). And so the bracket would not be executed, even if the variable ''$items'' should be used completely different the second time. 
 + 
 +This example is taken from chapter [[:en:create:rotation]]. There you will also find examples of further applications. 
 + 
 + 
 +===== Drag items of a question =====
  
 In the following example, on page 1, three items are taken from the items in question "AB01". Following this, the question is asked with these three items -- and, on a later page, question "ABO2" is asked with these exact same three items. In the following example, on page 1, three items are taken from the items in question "AB01". Following this, the question is asked with these three items -- and, on a later page, question "ABO2" is asked with these exact same three items.
Line 18: Line 54:
 question('AB01', $itemlist); question('AB01', $itemlist);
 // Register variable $itemlist // Register variable $itemlist
-registerVariable('itemlist');+registerVariable($itemlist);
 </code> </code>
  
Line 28: Line 64:
  
  
-===== Example 2 =====+===== Remember partial results/filter =====
  
 On page 4 in the questionnaire, a rather complicated filter is used to determine whether the participant falls into group 1 (prospective clients or clients preparing to do business), or group 2 (current clients). Different questions will be shown later on the in the questionnaire resulting from this classification. On page 4 in the questionnaire, a rather complicated filter is used to determine whether the participant falls into group 1 (prospective clients or clients preparing to do business), or group 2 (current clients). Different questions will be shown later on the in the questionnaire resulting from this classification.
Line 41: Line 77:
   $group = 2;   $group = 2;
 } }
-registerVariable('group');+registerVariable($group);
 </code> </code>
  
Line 67: Line 103:
  
  
-===== Example 3 =====+===== Content Rotation =====
  
 4 questions ("AB01" to "AB04") are displayed with their associated text elements ("text1" to "text4) -- but in random order. Therefore, an array is shuffled on page 1 of the questionnaire and made available for the following pages with ''registerVariable()''. For further details, please see chapter [[:en:create:rotation|Rotation]]. 4 questions ("AB01" to "AB04") are displayed with their associated text elements ("text1" to "text4) -- but in random order. Therefore, an array is shuffled on page 1 of the questionnaire and made available for the following pages with ''registerVariable()''. For further details, please see chapter [[:en:create:rotation|Rotation]].
Line 79: Line 115:
 ); );
 shuffle($questions); shuffle($questions);
-variableRegister('questions');+registerVariable($questions);
 </code> </code>
  
en/create/functions/registervariable.txt · Last modified: 29.09.2020 17:28 by sophia.schauer
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
Driven by DokuWiki