Jump to content

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.
Link to comment
https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/
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
Link to comment
https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14655
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>"

Link to comment
https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14667
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
Link to comment
https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14713
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
Link to comment
https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14763
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.