# TOPIC: Iterate through questions to filter answers

## Iterate through questions to filter answers 3 years 1 week ago #88810

 garf Offline Fresh Lemon Posts: 8 Karma: 0 Hi all, I've been playing with LimeSurvey 2 of late and I have to say I'm totally impressed! I've run into a situation though and I'm wondering if anyone can help? To keep it simple: I have 2 ranking type questions(the real survey has 42). I have 4 pools of possible answers for each ranking, say A,B,C,D. Each answer code begins with one of these letters, so, A1, B1, C1, D1 - A2, B2, C2, D2 I only want to score items ranked in 1'st and 2'nd place. Whatever pool letter is Ranked 1 will get 5 points, 2nd place 3 - 4th and 5th are irrelevant. At the end of the survey, I want to summarize scores/totals across the questions pools for A, B, C D . -- -- -- -- Q1 Q2 Rank 1 -- A A Rank 2 -- C B -- -- -- -- --Q1-- -- Q2 Rank 1 -- A=5 -- A=5 Rank 2 -- C=3 -- B=3 = A=10, B=3, C=3, D=0 I tried using something like this to give me some totals if(substr(Q1_1, 0, 1) == "A", ATotal=ATotal + 5, if(substr(Q1_1, 0, 1) == "B", BTotal=BTotal + 5, if(substr(Q1_1, 0, 1) == "C", CTotal=CTotal + 5, if(substr(Q1_1, 0, 1) == "D", DTotal=DTotal + 5, 0)))) if(substr(Q1_2, 0, 1) == "A", ATotal=ATotal + 3, if(substr(Q1_2, 0, 1) == "B", BTotal=BTotal + 3, if(substr(Q1_2, 0, 1) == "C", CTotal=CTotal + 3, if(substr(Q1_2, 0, 1) == "D", DTotal=DTotal + 3, 0)))) ..but I get scores like 39 and 43 etc... So it seems like the code keeps getting triggered when there is activity in the survey. Has anyone seen this before or have some suggestions how to handle this? The problem is when you have 42 answers x 4, the equation gets really long. Is there a way to filter or iterate through the question perhaps with a scripted loop or something? Thanks for any guidance in advance! G The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 1 week ago #88826

 DenisChenu Offline Moderator Lime Posts: 7881 Thank you received: 1081 Karma: 313 Just an idea, After each ranking (the number is X, example for first and A), add 4 equation question (attribute : numeric):```CODE : SUMA01 Text : {(if(Q1_1=="A1",5,if(Q1_2=="A1",4,0))} CODE : SUMB01 Text : {(if(Q1_1=="B1",5,if(Q1_2=="B1",4,0))} CODE : SUMC01 Text : {(if(Q1_1=="C1",5,if(Q1_2=="C1",4,0))} CODE : SUMD01 Text : {(if(Q1_1=="D1",5,if(Q1_2=="D1",4,0))}```For the second```CODE : SUMA02 Text : {(if(Q2_1=="A2",5,if(Q2_2=="A2",4,0))} CODE : SUMB02 Text : {(if(Q2_1=="B2",5,if(Q2_2=="B2",4,0))} CODE : SUMC02 Text : {(if(Q1_1=="C2",5,if(Q1_2=="C2",4,0))} CODE : SUMD02 Text : {(if(Q1_1=="D2",5,if(Q1_2=="D2",4,0))}```(each equation are the same, just change X. At the end of the survey: CODE : TOTALA Text : {sum(SUMA01,SUMA02,SUMA03 .... )} It's more easy to do, just a long way, but you already adding 42 ranking question. Denis Assistance on LimeSurvey forum and LimeSurvey core developpement are on my free time (Say thanks ?). A bug not reported is a bug not corrected. | Please, read the documentation | La doc en français à besoin de vous Official LimeSurvey Partner | Web development | SkeletonQuest, RWD for LimeSurvey The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 6 days ago #88906

 garf Offline Fresh Lemon Posts: 8 Karma: 0 Thanks Denis for the great idea! I have to admit, I didn't even think to break it down that way. In the interest of expanding my knowledge, would you or someone else here on the forum know, if I did want to run a script after the last question, say something like below; given quesiton code is A1_1. A2_2 ..etc. Is it possible to access the questions this way? ```       ``` Thanks again for your help!! Garf The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 6 days ago #88931

 tpartner Offline LimeSurvey Team Posts: 5270 Thank you received: 1098 Karma: 457 It looks to me like the loop should work, but I think the question codes should be Q1_1, Q1_2... Cheers, Tony Partner Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise. LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project. The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 5 days ago #88970

 DenisChenu Offline Moderator Lime Posts: 7881 Thank you received: 1081 Karma: 313 `str= "{Q" + i + "_1}"`Thinks EM manager don't work here. EM mangaer look at`Q" + i + "`vars, and this var donesn't exist. Denis Assistance on LimeSurvey forum and LimeSurvey core developpement are on my free time (Say thanks ?). A bug not reported is a bug not corrected. | Please, read the documentation | La doc en français à besoin de vous Official LimeSurvey Partner | Web development | SkeletonQuest, RWD for LimeSurvey The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 5 days ago #88972

 DenisChenu Offline Moderator Lime Posts: 7881 Thank you received: 1081 Karma: 313 ```var questionArray=new Array("{Q1_1","Q1_2","Q1_3" ... ); len=questionArray.length; answer = null; for (var i = 0; i < len; i++) { answer = questionArray[i]; switch(answer.substring(0,1)) { case "A": AT=AT+5; break;   case "B": BT=BT+5; break;   case "C": CT=CT+5; break;   case "D": DT=DT+5; break;   } }```Don't look at the number of {/} maybe some error. Denis Assistance on LimeSurvey forum and LimeSurvey core developpement are on my free time (Say thanks ?). A bug not reported is a bug not corrected. | Please, read the documentation | La doc en français à besoin de vous Official LimeSurvey Partner | Web development | SkeletonQuest, RWD for LimeSurvey The administrator has disabled public write access.

## Iterate through questions to filter answers 3 years 5 days ago #89010

 garf Offline Fresh Lemon Posts: 8 Karma: 0 Thanks Denis! That solved the mystery for me... I was having a problem with version 2 and direct script in the question with {INSERTANS:SQGA}. It kept filling up my script with html etc. The {qcode} works perfectly in script and the array solution makes perfect sense. Thanks again for your help! Garf The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.179 seconds