0 votes
in SoSci Survey (English) by s293908 (335 points)
edited by SoSci Survey

Hi,

Problem description:
I fetch a value with dbGet($keyGroup) and assign it to $groupA. When I debug, $groupA clearly shows "A-KO". However, in my if / elseif logic (if ($groupA == 'A-KO')), the code always falls through to the else branch.

Further Debugging:
Hard-coding $groupA = 'A-KO'; works as expected.
So it seems the value from dbGet() contains hidden characters or a different format than a plain string.

Question:
What exact data type or format does dbGet() return, and how should I reliably compare the result (e.g. to "A-KO") inside PHP?

My Code:

///////////// Get Group of Person A /////////////

// get group of person A: A-FK, A-MA, A-KO
$keyGroup = 'GROUP-' .reference(); // from link ?r=%caseSerial%
$groupA   = dbGet($keyGroup);



///////////// Set Group Person B /////////////

// define group of person B
// A: Führungskraft (A-FK) -> B: Mitarbeiter*in (B-MA)
if ($groupA == 'A-FK') {
    $groupB = 'Mitarbeiter*in'; // display label
    setPageOrder('MA1', 'MA2', 'end');

// A: Mitarbeiter*in (A-MA) -> B: Führungskraft(B-FK)
} elseif ($groupA == 'A-MA') {
    $groupB = 'Führungskraft'; // display label
    setPageOrder('FK1', 'FK2', 'end');

// A: Kollege*in (A-KO) -> B: Kollege*in (B-KO)
} elseif ($groupA == 'A-KO') {
    $groupB = 'Kollege*in'; // display label
    setPageOrder('KO1', 'KO2', 'end');

// else go to error page
} else {
    $groupB = '-'; // fallback if no mapping exists
    setPageOrder('error', 'end');
}

// debug
replace('%groupB%', $groupB);
replace('%groupA%', $groupA);
by SoSci Survey (356k points)
> When I debug, $groupA clearly shows "A-KO".

Please kindly post the debug information after adding this line:

$groupA   = dbGet($keyGroup);
debug($groupA);  // THIS HERE
by s293908 (335 points)
when I debug (debug($groupA); ) this is the output:

Debug-Information für $groupA:
$groupA = false (boolean)

However, if I debug (replace('%groupA%', $groupA);) in the actual pretesting, where information is stored in the "Datenbank für Inhalte" it returns: A-KO.
by SoSci Survey (356k points)
Ih cannot tell details about the replace() result, because I do not see the context. However, if debug($groupA) tells us that "false" is returned in this moment, it explains why the filter does not work as expected.

If it's just a quetion about "actual pretesting", then please run the pretest in debug mode to get all the necessary details. Or start the page in debug mode, and just add an r=... to the URL in the browser address. Please post the debug output (preferrably the full debug information of the page) for the pretest that should receive a group.

You may want to add this line, as well:

debug($keyGroup);
by s293908 (335 points)
Hi,

thanks for the quick response. I want to clarify the problem I’m seeing with $groupA and the group handling:

1. Debug Mode
When I run the survey in debug mode, this is the output:
[Information] Interview gestartet (neue Nummer 1318)
[Information] Der Fragebogen t00-B im Projekt StressorCreation2 wird verwendet
[Verarbeitung] Erstelle Seite 13 in Fragebogen t00-B
[Information] Entsprechend setPageOrder() werden nun folgende Seiten gezeigt:
14 [error], Ω (end)
debug() Inhalt: $groupA = false (boolean)
debug() Inhalt: $keyGroup = GROUP- (string)
[Information] Folgende Platzhalter sind vorbereitet:
%groupB% = (Text) -
%groupA% = (Text)
%keyGroup% = (Text) GROUP-

So in this situation, $groupA is false, and $keyGroup is just "GROUP-". I assume bc the debug mode does not access the "Datenbank für Inhalte"?

2. Pretest (clicking through as a participant)
When I activate the survey and test it like a normal respondent, the placeholders return values from the “Datenbank für Inhalte”:
replace('%groupB%', $groupB);
replace('%groupA%', $groupA);
replace('%keyGroup%', $keyGroup);

Output:
$groupB: -  
$groupA: A-FK  
$keyGroup: GROUP-CARCT7H2T2

This matches the entry in the content database:
GROUP-CARCT7H2T2    ger    A-FK    23.09.2025, 16:37

So the content is definitely stored and retrievable.

3. The Dilemma
Even though %groupA% correctly resolves to A-FK, the if/else condition in my code acts as if $groupA is false and runs the fallback branch:

} else {
    $groupB = '-'; // fallback if no mapping exists
    setPageOrder('error', 'end');
}

If I hardcode, for example, $groupA = 'A-FK; the condition works as expected setting the following pages: MA1, MA2, end. Here the debug:
[Information]    Interview gestartet (neue Nummer 1322)
[Information]    Der Fragebogen t00-B im Projekt StressorCreation2 wird verwendet
[Verarbeitung]    Erstelle Seite 13 in Fragebogen t00-B
[Information]    Entsprechend setPageOrder() werden nun folgende Seiten gezeigt:
17 [MA1], 18 [MA2], Ω (end)
debug()    Inhalt: $groupA = A-FK (string)


This leads me to think that the value retrieved from dbGet() might contain hidden characters (whitespace, newline, encoding issues) so it does not match 'A-FK' or 'A-KO' in a strict comparison.

4. My Question
Is there a recommended way to normalize or check values coming from the content database (e.g. trim() )? Or is there a better way to debug what exactly $groupA contains at runtime, beyond just debug($groupA);?

1 Answer

0 votes
by SoSci Survey (356k points)

Well, $groupKey seems to be empty, because you did not specify a reference in the URL. As I wrote above: Start in debug mode, then add &r=... to the URL in the browser's address bar, and press enter. As an option, you can use the yellow/orange debug bar and set the REF variable properly.

And another option: Add html($groupA) to the PHP code, to also see the variable contents in non-debug mode.

Well, probably, I found the error, anyway. dbGet() will always (!) return an array. So you filter cannot match. Please try replacing

$groupA   = dbGet($keyGroup);
html($groupA);  // Just for debugging

by

$data   = dbGet($keyGroup);
$groupA = $data[0];
html($groupA);  // Just to be sure
by s293908 (335 points)
Ah perfect, it works with $groupA = $data[0]; !

I however get this waring, is it okay to ignore it?

Im PHP-Code trat ein Fehler auf.
Fehler im Fragebogen: Trying to access array offset on false
Zeile: 8

PHP-Code

005  
006 $keyGroup = 'GROUP-' .reference();  
007 $group   = dbGet($keyGroup);
008 $groupA   = $group[0];
009
010  
011
by SoSci Survey (356k points)
You shall include a little fallback for the case that dbGet() returns false:

$data   = dbGet($keyGroup);
if ($data) {
  $groupA = $data[0];
} else {
  $groupA = '';
  // Maybe show an error message
}
by s293908 (335 points)
Done - thank you so much!

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

...