Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Using php/mysql for calculations

New Here ,
Jan 24, 2012 Jan 24, 2012

Hi guys,

Im experimenting with calculations on a site but im not sure whats the right way to go about it.

the result im looking for is this,,

Im setting up a personality test on the website where the questions he answers should classify him into one of 4 groups.

my thoughts are to keep it simple, every question he answers should have a +1 effect on the certain group in question and then at the end of all the questions display the result of the group that has the largest count.

now some told me i should use javascript which im not very familiar with,

others said i should be able to do the calculations in php before the result is sent to the database

but i also thought of the possibility to send all into uncalculated to the db and then just retrieve the right result,

would like to hear your opinions on this,

or if you think there is a easier way than my "+1 effect"

thanks in advance!

TOPICS
Server side applications
842
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 25, 2012 Jan 25, 2012

One way would be to associate each question with a group.

Create an array with 4 elements say G(0), G(1), G(2), G(3)  Set it to zero. 

for question = 1 to number of questions

      Ask a question

          Each time a question is answered correctly,  increment value of the associated array group element (1) to (4) by 1.

           Until you have asked the first round of questions.

               So now you have a set of values in each element. Problem is how to find the largest. Especially if you have a tie.

          Sort  the array to set them in order ascending- see :http://www.php.net/manual/en/function.asort.php


         set $winner to the first groups reference  ( this will always be 0,1 2 or 3 and should contain the winner unless there is a tie)

          If the lst two elements in the sorted list are not equal

                    set $winner to the 1st elements reference (if its G(3) set it to 3, not the value of G(3))

               else (you have a tie)

                         repeat

                              set the other two elements to zero

                              ask a  question

                              increment the matching group element

                               sort the array

                              compare the values of the two top elements

                          until the values of  two top elements are not equal

               set the  value of $winner to the 1st elements reference (if its G(4)set it to 4, not the value of G(4))

 

                     store $winner in your database

Hope this makes sense - it took a while to work out and it still needs coding.

The other way is to ask your questions and save them in the database as you go along. At the end of stage one, do a select all from groups, order by group value

You may then have a tie, so you would need to ask more questions as deciders and do it again.

Either way, ist an interesting project.

I'm off to bed!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 26, 2012 Jan 26, 2012
LATEST

>but i also thought of the possibility to send all into

>uncalculated to the db and then just retrieve the right result,

Ding ding ding! You got it right. Store the raw data, then you can easily do the calculation using either SQL or PHP. If you just store the results, you lose valuable information and makes it difficult to do more in depth analysis. Of course, it's important that your database structure is setup correctly. I would set it up similar to a survey database:

UserResponseTable:

userId

questionID

response

QuestionTable:

questionID

questionText

groupID

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines