Jump to content


Photo

Adding up test answers


  • Please log in to reply
5 replies to this topic

#1 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 March 2006 - 04:06 PM

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.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 March 2006 - 04:36 PM

Try something like this:
<?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>';
}
?>
Please note that I have checked the code for syntax errors, so there may be some.

Ken

#3 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 March 2006 - 05:26 PM

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>"



#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 March 2006 - 06:53 PM

I goofed in my code, I used the variable $k instead of $v for the default case.

Try this instead:
<?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>';
}
?>

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

#5 grumpy

grumpy
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 06 March 2006 - 08:02 PM

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?



#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 March 2006 - 08:30 PM

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:
<?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]++;
?>

Ken





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users