grumpy Posted March 6, 2006 Share Posted March 6, 2006 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| ....question2023|jonn doe| a | c | .... b45|jay smith| b | d | .... bI don't know where to begin. Any help would be appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/ Share on other sites More sharing options...
kenrbnsn Posted March 6, 2006 Share Posted March 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14655 Share on other sites More sharing options...
grumpy Posted March 6, 2006 Author Share Posted March 6, 2006 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>" Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14667 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2006 Share Posted March 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14713 Share on other sites More sharing options...
grumpy Posted March 6, 2006 Author Share Posted March 6, 2006 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] => 1Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14751 Share on other sites More sharing options...
kenrbnsn Posted March 6, 2006 Share Posted March 6, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4215-adding-up-test-answers/#findComment-14763 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.