Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Multiple question to count the numbers

  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago - 4 years 1 week ago #196989 by matthewpulis
Multiple question to count the numbers was created by matthewpulis
I want to do a survey where for each question I give 2 options, but then I need to count how many K,A,E there are in the whole question group. Each question will be formed by two radio buttons and will have a code of K, A, E depending on the statement. Ultimately I need to count the total of Ks, As and Es chosen.


A working example is here: matthewbarr.co.uk/bartle/ - probably it's better if one views the source. I have done it in PHP: pastebin.com/sVASLzRz and now want to move it to LimeSurvey.

This is what I did:

I created a Question Group - 'test'
Created 3 questions/variables: explorer, achiever, killer.
Created 3 pairs of multiple choice questions T1, T2, T3.
The answer codes are 'A' and 'E' for T1.
I created another variable called result, and did this logic in it:
Code:
{if(T1.NAOK == "A", achiever.NAOK = sum(achiever.NAOK, 1), if(T1.NAOK == "E", explorer.NAOK = sum(explorer.NAOK, 1)))}

I did another expression which shows the results.
This is result:

results
Code:
t1: {T1.NAOK}
 
t2: {T2.NAOK}
 
t3: {T3.NAOK}
 
 
explorer {explorer.NAOK}
 
achiever {achiever.NAOK}
 
killer {killer.NAOK}


I am noticing that when I load the question explorer and achiever are already '2'.

The moment I choose an option, whatever the option, both increment by 2. Am flabbergasted as to why?


I am attaching the .lsg


Many thanks for your help!!
Last edit: 4 years 1 week ago by matthewpulis. Reason: forgot the {}
The topic has been locked.
  • holch
  • holch's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 week ago #196994 by holch
Replied by holch on topic Multiple question to count the numbers
Please always include an LSS file, not LSGs or LSQs.

For LSGs/LSQs the helpers in the forum need to create a survey to import it to, and hope that the base language of their survey is the same as the one in your files, because LSGs and LSQs are not language independent.

Also, the problem might be related to other settings in your survey, which the helpers won't notice, as they created a new survey with maybe different settings.

But please, do not just dump the whole survey, but only the parts that are relevant to your problem.

So please, copy your survey, delete all unnecessary overhead. Than check if the problem still persists (sometimes this already solves the problem and you need to check which other settings influence the problem). If the problem persists, the cleaned version as LSS and upload it here.

Help us to help you. Make it easy for us.

I answer at the LimeSurvey forum in my spare time, I'm not a LimeSurvey GmbH employee.
No support via private message.

The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 week ago #196997 by tpartner
Replied by tpartner on topic Multiple question to count the numbers
I think you have the wrong syntax for your question assignment statements.

Code:
{qCode = if(condition, resultIfTrue, resultIfFalse)}

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The topic has been locked.
  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago #196998 by matthewpulis
Replied by matthewpulis on topic Multiple question to count the numbers
Thanks for your reply holch.

Attached is the LSS. I created the Test survey where there is the test group for which I sent.

Many thanks
The topic has been locked.
  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago #196999 by matthewpulis
Replied by matthewpulis on topic Multiple question to count the numbers
But since I am assigning different variables, how can I do that?

Thanks!
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 week ago - 4 years 1 week ago #197001 by tpartner
Replied by tpartner on topic Multiple question to count the numbers
You cannot assign variables in ExpressionScript in the normal way. All variables must be loaded into equation type questions, then you reference those question codes.

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 1 week ago by tpartner.
The topic has been locked.
  • tpartner
  • tpartner's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 week ago - 4 years 1 week ago #197002 by tpartner
Replied by tpartner on topic Multiple question to count the numbers
ExpressionScript does not assign values to questions dynamically, it is only done on page load and page submit. Therefor you will need to move the report question to a different group.

Additionally, in your result question (which assigns values to explorer, achiever and killer) I think you want something like this:

Code:
{explorer = sum(if(T1 == 'E', 1, 0), if(T2 == '2E', 1, 0))}
{achiever = sum(if(T1 == 'A', 1, 0), if(T2 == '2A', 1, 0), if(T3 == '3A', 1, 0))}
{killer = if(T3 == '3K', 1, 0)}

Sample survey attached:

File Attachment:

File Name: limesurvey...5171.lss
File Size:22 KB



...but, why use assignments at all? Why not load explorer, achiever and killer directly?

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
Last edit: 4 years 1 week ago by tpartner.
The following user(s) said Thank You: matthewpulis
The topic has been locked.
  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago #197024 by matthewpulis
Replied by matthewpulis on topic Multiple question to count the numbers
Tony,

Many thanks for your reply! That worked fine.

However, I'm afraid, I didn't understand your final suggestion: what do you mean load the variables directly?

I am understanding that from the working example which you sent me, I can save as part of the survey data, each of the answers and the computed answers of explorer, achiever, killer. Is that correct?

Once more many thanks for your help!!
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
4 years 1 week ago - 4 years 1 week ago #197027 by Joffm
Replied by Joffm on topic Multiple question to count the numbers
And if you use "better" codes in your answer options, you could do it easier.
If you - in all questions - code
achievers: 1
explorers: 2
killers: 3

you may use the implemented function "countifop"

achiever: {countifop("==",1,T1.NAOK,T2.NAOK,T3.NAOK)}

explorer: {countifop("==",2,T1.NAOK,T2.NAOK,T3.NAOK)}

killer: {countifop("==",3,T1.NAOK,T2.NAOK,T3.NAOK)}


Enter these equations in three questions of type equation as you have in your example at the start.

Joffm

Regarding your question: I think Tony wanted to explain, that it is not necessary to have these three equations at the start and later assign values to them.
Place these equations after your questions ent enter directly the formula, like
in equation "explorer"
{sum(if(T1 == 'E', 1, 0), if(T2 == '2E', 1, 0))}
in equation "achiever"
{sum(if(T1 == 'A', 1, 0), if(T2 == '2A', 1, 0), if(T3 == '3A', 1, 0))}

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 4 years 1 week ago by Joffm.
The following user(s) said Thank You: matthewpulis
The topic has been locked.
  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago #197033 by matthewpulis
Replied by matthewpulis on topic Multiple question to count the numbers
That is a much simpler way of doing it Joffm! Thanks loads!

Thanks for explaining what tonym said. Many many thanks to both of you. Will update the thread with the final implementation. Many thanks!
The topic has been locked.
  • matthewpulis
  • matthewpulis's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
4 years 1 week ago #197038 by matthewpulis
Replied by matthewpulis on topic Multiple question to count the numbers
I'm uploading the final version of my test, just in case someone else would need it later.

JeffM's is the simplest way, especially when IRL case, you need a number of questions (this test IRL will be over 30 qs) - so it is much simpler.

Thanks everyone who contributed to this thread. Much appreciated!!
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose