Jump to content

Archived

This topic is now archived and is closed to further replies.

grumpy

Adding up test answers

Recommended Posts

Hello-
I have a table, called 'users', where results from a 20 question mulitple-choice test are kept. Each question could have any one of four answers,:a, b, c, d. I'm trying to figure out a way to find out how many of the 20 answers to the test for a particular user are 'a', how many are 'b', etc.
Here is how my table is setup, with two sample rows:

ID|username|question1|question2| ....question20
23|jonn doe| a | c | .... b
45|jay smith| b | d | .... b

I don't know where to begin. Any help would be appreciated. Thank you.

Share this post


Link to post
Share on other sites
Try something like this:
[code]<?php
//
// assume db connections are here
//
$q = "select * from `users`";
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());
while ($rw = mysql_fetch_assoc($rs)) {
   $counts = array('a'=>0,'b'=>0,'c'=>0,'d'=>0);
   foreach ($rw as $k => $v)
       switch ($k) {
           case 'ID':
           case 'username': // add any field names here which are not "question<nn>"
               break;
           default:
               $counts[$k]++;
       }
   echo '<pre>For username: '.$rw['username']. ': ' . print_r($counts,true) . '</pre>';
}
?>[/code]
Please note that I have checked the code for syntax errors, so there may be some.

Ken

Share this post


Link to post
Share on other sites
Thanks for your answer. I guess I still am not doing something right. When I use your code I just get a page listing all the fields, all with values of "1", like this:

[question10] => 1
[question11] => 1
[question12] => 1
.. etc.

Any idea why? I'm not sure I understand the line : case 'username': // add any field names here which are not "question<nn>"

Share this post


Link to post
Share on other sites
I goofed in my code, I used the variable $k instead of $v for the default case.

Try this instead:
[code]<?php
//
// assume db connections are here
//
$q = "select * from `users`";
$rs = mysql_query($q) or die('Problem with query: ' . $q . '<br>' . mysql_error());
while ($rw = mysql_fetch_assoc($rs)) {
   $counts = array('a'=>0,'b'=>0,'c'=>0,'d'=>0);
   foreach ($rw as $k => $v)
       switch ($k) {
           case 'ID':
           case 'username': // add any field names here which are not "question<nn>"
               break;
           default:
               $counts[$v]++;
       }
   echo '<pre>For username: '.$rw['username']. ': ' . print_r($counts,true) . '</pre>';
}
?>[/code]

Basically, you only want to look at the fields labeled question1 through question20, so you put any field names in the first section with the case statement for each field. The "break" with no other code, says don't do anything. Then the default action will apply to all other fields, i.e. the questions.

Ken

Share this post


Link to post
Share on other sites
Thank you again! It works! There is one small problem still. Some of the totals are split over two lines in the results. For example, if A is the answer 9 times, the results might appear as:
[ A] => 8
[ A] => 1

Any ideas?

Share this post


Link to post
Share on other sites
Without seeing a dump of the data it is hard to tell what the problem might be. The only thing I can think of is that the strings that look like the same character really aren't.

You can try putting the following error checks in and see if any data gets kicked out:
[code]<?php
           default:
               $v = trim($v);
               if (strlen($v) > 1 || (strlen($v) == 1 && (ord($v) < 65 || ord($v) > 68))) {
                     echo "The recorded answer to $k is not within range (A - D):<br>\n";
                     if (strlen($v) > 1) echo 'strlen: ' . strlen($v);
                     if (ord($v) < 65 || ord($v) > 68) echo 'Wrong character in DB:' . $v  . '[' . ord($v) . ']'. "<br>\n";
               }
               $counts[$v]++;
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.