Welcome, Guest
Username: Password: Remember me

TOPIC: Generating Labels in Database using EM

Generating Labels in Database using EM 2 years 7 months ago #74747

I have a quick question about the EM.
I have created a hidden equation question in which I give several conditions that should display different text depending on the responses to other questions.
My code doesn't return anything in the database, and I'm not sure why. Can someone look at my code and make sure the syntax is correct? These numbers are based on scores being returned by the assessment feature.

The code is as follows:
{if(!is_empty(PFTotals), if(PFTotals >= -5 && PFTotals <= -4, 'Very Liberal', if(PFTotals >= -3 && PFTotals <= -2, 'Liberal', if(PFTotals == -1, 'Leans Liberal', if(PFTotals == 0, 'Moderate', if(PFTotals == 1, 'Leans Conservative', if(PFTotals >= 2 && PFTotals <= 3, 'Conservative', if(PFTotals >= 4 && <= 5, 'Very Conservative')))))))}

Screen Shot 2012-02-20 at 4.29.39 PM.png
The administrator has disabled public write access.

Re: Generating Labels in Database using EM 2 years 7 months ago #74748

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
FreedomTrip-

If you look at your attached screenshot, you'll see that the first "if" is surrounded by a red line. That means there is a syntax error. If you hover your mouse over that word, you'll see a message saying what the problem is. In your case, it says "Parentheses not balanced", which means that you are missing at least one closing parenthesis. There are some other errors too.

Deeply nested if statements work fine, but can take a little effort to get correct. By counting open and closing parentheses, and saving (and seeing any errors that show up), you can quickly get it right. Please note that EM may only report one error message, even if there is more than one.

For especially deep nesting, the easiest solution is to use a text editor and compose the nested if statements on multiple lines so that you can more easily align the parentheses.

It used to be that you would then have to convert the equations to fit onto on line. However, with a recent fix for a different problem, you can now have EM expressions span lines, as long as you make sure to use source mode in the editor (to avoid having LimeSurvey add <div> or <br/> for each line). So, after fixing the couple of errors, a working version of your expression is this:
{if(is_empty(PFTotals),
	'',
	if(PFTotals >= -5 && PFTotals <= -4,
		'Very Liberal',
		if(PFTotals >= -3 && PFTotals <= -2,
			'Liberal',
			if(PFTotals == -1,
				'Leans Liberal',
				if(PFTotals == 0,
					'Moderate',
					if(PFTotals == 1,
						'Leans Conservative',
						if(PFTotals >= 2 && PFTotals <= 3,
							'Conservative',
							if(PFTotals >= 4 && PFTotals <= 5,
							'Very Conservative',''
							)
						)
					)
				)
			)
		)
	)
)}

Remember that there can be no space (or newline) between the opening curly brace and the next character; and there can be no whitespace before the closing curly brace.

So, the core problems in your expression were:
(1) You were missing one closing parenthesis
(2) You were missing the final if option (e.g. if not 'Very Conservative', then show '')

/Tom
Last Edit: 2 years 7 months ago by TMSWhite.
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.161 seconds
Donation Image