php - Better approach to get result of answers -


i have multiple questions answerable yes or no this:

  1. question 1: yes no
  2. question 2: yes no
  3. question 3: yes no
  4. question 4: yes no
  5. question 5: yes no
  6. question 6: yes no

i need results of combination of "yes" answers, did traditional if else this;

if($_post[q1] && $_post[q2] && $_post[q5]){     echo 'something'; }elseif($_post[q3] && $_post[q5]){     echo 'something again'; }elseif($_post[q3] && $_post[q5]){     echo 'something again again'; } , on... 

although correct, still produces hundreds of lines of code because there's many questions , possible combinations.

is there better approach? ty guys..

edit:

ah sorry second copy>paste haha, anyway... there's 37 questions, , there's many combinations if 1 , 2 yes this... if 2 , 3 , 4 yes this... don't need every combination.

i read somewhere can use 2,4,8,16,32,64,128 etc... (i don't know called haha) add result. if 1 & 2 yes result 6 because 2 + 4 = 6, or if 4 & 6 yes result 80 because 16 + 64 = 80... can this

question 1 = <input type=text value=2 name=que[] /> question 2 = <input type=text value=4 name=que[] /> question 3 = <input type=text value=8 name=que[] /> question 4 = <input type=text value=16 name=que[] /> question 5 = <input type=text value=32 name=que[] /> question 6 = <input type=text value=64 name=que[] />  $array = (   6=>"1&2 yes",   10=>"1&3 yes",   80=>"4&6 yes" );  echo $array[array_sum($_post[que])]; 

is approach? or there better?

this pretty tricky!

i have experience in , after 3 goes. come solution. think trying build "recommendation engine". ask several questions , provide answer, recommendation.

you may have 37 questions, suspect or client has 20 answers, , bet keen on changing them! or being able see answers are.

what did solve create "logic file" each line evaluated match answer yes or no.

so lets have 37 questions if question1 yes lets use q1 = true in code below.

so lets build list. our code go through each line try , find match top bottom. once finds match stops looking more matches.

  1. the trick put complex matches @ top.
  2. put simple matches @ bottom.
  3. make sure have "catchall matches" there no way end of file without matching something.
  4. each line of code points answer. answer index webpage, text, solution, lets call a1
so put tricky match @ top. a10 | q1 & q2 & (q3 or q4) # means q1=yes, q2=yes, q3=yes or q4=yes a9  | q1 & q2 & *q3  # use * mean not q3, eg q3 must false  a8  | q1  a7  | q2  a6  | *q1 

if answered q1=yes , other questions no. a8 match. if q1 = yes, q2 = yes other no, a9 match

you have code logic engine starting idea. logic engine have parse custom logic file , make logic work isnt toooo hard. fun.

having started or'ing bits, 2,4,8,16 thing eluding far hard debug. did @ start of ours. sucks...

we wrote 100's of "if" statements, have sit down client beside , try , come logic. doesnt work , takes long time debug.

this logic file great. can see there billions of combinations file based system can see how arrive @ each solution, combinations must meet. , if not met, rule should evaluated next.

quick test here. 37 yes no questions there 2^(n-1) solutions = 68,719,476,736

so logic file quick way break down known combinations want catch , see being caught (eg line present in logic file).

here logic engine in python.

class logicengine(object):      def filtertags(self, rules, tags):         if(rules.strip()==""): return true # no tags means true.         # bracket logic, eg...   *a & (b or c)         openbracket = 0         closebracket = 0         while(openbracket != -1):                    openbracket = rules.find("(",openbracket)             closebracket = rules.find(")",openbracket+1)             if(openbracket!=-1):                 # found rule, convert *a & (b or c) *a & true, can handled logic system.                 subrule = rules[openbracket+1:closebracket]                 if self.filtertags(subrule,tags):                     rules = rules.replace("(" + subrule + ")", "true")                     #print rules                 else:                      rules = rules.replace("(" + subrule + ")", "false")                     #print rules          #print rules #quick check.         rulelist = rules.split("or")          rule in rulelist:             rule = rule.replace(" ","")             rule = rule.split("&")             if self.checkrule(rule, tags) : return true          return false #no matches found means not valid.      def checkrule(self, rules, tags):         rule in rules:             if rule[0:1]=="*":                 # not rule.                 rule = rule[1:] #remove star                 if rule =="true":                      return false                 if rule =="false":                     # nothing, keep looping.                     pass                 elif (listcontains(tags,rule)):                     return false             else:                 if rule =="false":                      return false                 if rule =="true":                     # nothign                     pass                 elif (not listcontains(tags,rule)):                     return false         return true  def listcontains(list, key):     try:         list.index(key)         return true     except valueerror:         return false 

so in short, going need work here make go. it's not simple.

there worst case turn $_post bit more readable.

then run big list of if statements complex @ top.

eg

// answer1.html people hairy hands. if($q1 && $q2 && !$q3 || ($q6 && $q7) return "answer1.html";

// answer2.html people beards if($q1 && $q2 && $q3) return "answer2.html";

// answer3.html people nose hair if($q1) return "answer3.html";

// answer4.html people toe hair if(!$q1) return "answer4.html";

i still make these statments more expressive. instead of $q1, make $nosehair can read conditions , answers.

right luck.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -