This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:create:functions:put [16.01.2015 19:07] – [Example: Calculations] alexander.ritter | en:create:functions:put [11.02.2024 07:38] (current) – admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== put() ====== | ====== put() ====== | ||
- | '' | + | '' |
The function '' | The function '' | ||
Line 7: | Line 7: | ||
* // | * // | ||
* //value//\\ A number or text (string), which should be stored in the variable. Ordinarily, a variable with the corresponding value is given as the second parameter. | * //value//\\ A number or text (string), which should be stored in the variable. Ordinarily, a variable with the corresponding value is given as the second parameter. | ||
+ | * // | ||
+ | |||
+ | **Warning: | ||
+ | |||
+ | **Tip:** If you want to save not a single value but a whole array in the data set, use '' | ||
+ | |||
Line 18: | Line 24: | ||
- | ===== Example: Randomization ===== | ||
- | If you are using the command '' | + | ===== Calculations ===== |
- | + | ||
- | The following example assumes that you have created an internal variable with the ID " | + | |
- | + | ||
- | <code php> | + | |
- | // Draw a whole number between 1 and 2 | + | |
- | $number | + | |
- | // Store the number drawn in the data record | + | |
- | put(' | + | |
- | // And, depending on the number, display text element " | + | |
- | if ($number | + | |
- | text(' | + | |
- | } else { | + | |
- | text(' | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ===== Example: | + | |
The following example assumes that one construct is assessed in two scales with the question IDs AB01 and AB02 -- and that an internal variable AB03_01 was created in order to save the [[: | The following example assumes that one construct is assessed in two scales with the question IDs AB01 and AB02 -- and that an internal variable AB03_01 was created in order to save the [[: | ||
Line 63: | Line 50: | ||
- | ===== Example: Advanced Filter ===== | + | ===== Recode Answers ===== |
+ | |||
+ | Recoding values is typically done in the data analysis, but if you already need the recoded values in the interview (e.g. in a [[:en: | ||
+ | |||
+ | For example, if you want to recode the open-ended text input for age from the variable " | ||
+ | |||
+ | <code php> | ||
+ | $age = (int)value(' | ||
+ | if ($age < 0) { | ||
+ | put(' | ||
+ | } elseif ($age < 25) { | ||
+ | put(' | ||
+ | } elseif ($age <= 40) { | ||
+ | put(' | ||
+ | } else { | ||
+ | put(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The '' | ||
+ | |||
+ | |||
+ | ===== Transforming Codes ===== | ||
+ | |||
+ | SoSci Survey stores positive response codes for scales ([[: | ||
+ | |||
+ | **Note:** In practice, the recoding or transformation of values is usually part of the evaluation and is therefore performed in the statistics software. There are therefore only very special cases in which such a transformation makes sense already during the survey. | ||
+ | |||
+ | **Note:** To " | ||
+ | |||
+ | The following code transforms the answers for the scale item SK01_01 from the value range 1 to 5 into the range -2 to +2. If no valid answer was given for the scale item (item omitted or an alternative option selected), no value is stored in the data set. The PHP code is placed on the page after question SK01 (see notes on '' | ||
+ | |||
+ | Precondition is that an internal variable SK02_01 has been created in advance (see [[# | ||
+ | |||
+ | <code php> | ||
+ | $original = value(' | ||
+ | // Save only valid values (>0) | ||
+ | if ($original > 0) { | ||
+ | put(' | ||
+ | } else { | ||
+ | put(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The PHP code can also be made shorter (but less clear): | ||
+ | |||
+ | <code php> | ||
+ | $org = value(' | ||
+ | put(' | ||
+ | </ | ||
+ | |||
+ | If this transformation is to be performed for all items of the scale SK01 (and sufficient internal variables have been created in SK02), the following [[: | ||
+ | |||
+ | <code php> | ||
+ | $items = getItems(' | ||
+ | foreach ($items as $item) { | ||
+ | $original = value(id(' | ||
+ | if ($original > 0) { | ||
+ | put(id(' | ||
+ | } else { | ||
+ | put(id(' | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Advanced Filter ===== | ||
The following example is taken from a questionnaire that shows different questions at several points for new customers and existing customers. A new customer distinguishes himself by showing that he is yet to have bought anything (answer 1 in selection question " | The following example is taken from a questionnaire that shows different questions at several points for new customers and existing customers. A new customer distinguishes himself by showing that he is yet to have bought anything (answer 1 in selection question " | ||
Line 84: | Line 137: | ||
} else { | } else { | ||
question(' | question(' | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== Randomization I ===== | ||
+ | |||
+ | If you use '' | ||
+ | |||
+ | The following PHP code randomly arranges the values from 1 to 10: | ||
+ | |||
+ | <code php> | ||
+ | $elements = array(1, | ||
+ | shuffle($elements); | ||
+ | </ | ||
+ | |||
+ | The content of the mixed array '' | ||
+ | |||
+ | ^Index^0^1^2^3^4^5^6^7^8^9^ | ||
+ | |Element|7|2|10|5|9|4|8|3|6|1| | ||
+ | |||
+ | Die zugehörigen Ränge wäre dann: | ||
+ | |||
+ | ^Element^1^2^3^4^5^6^7^8^9^10^ | ||
+ | |Rang|10|2|8|6|4|9|1|7|5|3| | ||
+ | |||
+ | To save the sequence, you need a question of the type " | ||
+ | |||
+ | <code php> | ||
+ | for ($i=0; $i< | ||
+ | // Create internal variable ID from question ID plus index | ||
+ | $id = id(' | ||
+ | // Save the value from the array here | ||
+ | put($id, $elements[$i]); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | To the index '' | ||
+ | |||
+ | Often it is more useful to save the ranks of the elements. For example if '' | ||
+ | |||
+ | <code php> | ||
+ | for ($i=0; $i< | ||
+ | // Create internal variable ID from question ID plus element content (!) | ||
+ | $id = id(' | ||
+ | // Save the rank here | ||
+ | put($id, $i + 1); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Of course, this code only works if the elements are numerical elements that start counting with 1. To store the ranks of other array contents (e.g. page labels), you need a slightly different PHP code: | ||
+ | |||
+ | <code php> | ||
+ | $pages = array(' | ||
+ | // Do not mix the pages directly, but their positions | ||
+ | $indices = array_keys($pages); | ||
+ | shuffle($indices); | ||
+ | // Now save the ranks - and save the new sequence of pages in $random | ||
+ | $random = array(); | ||
+ | for ($i=0; $i< | ||
+ | // Save rank as above $id = id(' | ||
+ | put($id, $i + 1); | ||
+ | // Save page in sequence | ||
+ | $random[$i] = $pages[$indices[$i]]; | ||
+ | } | ||
+ | // Set new page sequence | ||
+ | // After the last page we will continue on page " | ||
+ | setPageOrder($random, | ||
+ | </ | ||
+ | |||
+ | ===== Randomization II ===== | ||
+ | |||
+ | If you work with the '' | ||
+ | |||
+ | **Important: | ||
+ | |||
+ | The following example assumes that you have created an internal variable with the ID " | ||
+ | |||
+ | <code php> | ||
+ | // Dice an integer between 1 and 2 | ||
+ | $nubmer = random(1, | ||
+ | // Save the diced number in the data set | ||
+ | put(' | ||
+ | // And depending on it, display text module " | ||
+ | if ($number == 1) { | ||
+ | text(' | ||
+ | } else { | ||
+ | text(' | ||
} | } | ||
</ | </ |