Donovan
Client

Joined: Oct 07, 2003
Posts: 735
Location: Ohio
|
Posted:
Mon Oct 04, 2010 9:32 am |
|
I have a page that displays correct answers to an exam. It shows a frequency count of answers, and the correct frequency count was bold. I need to add another column and add the correct answer in the form of the letter, (a,b,c,d,e). My problem lies in the fact that sometimes I have more than one correct answer, or the teacher will throw a question out and give everybody credit, meaning all answers are correct. I'm having trouble displaying this as it is passed to a template and the page said array in the column.
In the beginning $stat_points looks like this. 0#1#0#0#0#, which means "b" is correct answer.
Here is what I'm doing.
Code:$stat_points = $stats['points'];
$raw_points = explode ( "#", $stat_points);
array_pop($raw_points); // last element will always be empty because the points seperated by # and also end with #
|
So I end up like 01000 right?
I then do the following for my different question types.
Code:if ($stats['qtype'] == 1) {
$letters = array("a", "b", "c", "d", "e");
}elseif ($stats['qtype'] == 4){
$letters = array("a", "b", "c", "d", "e", "f", "g", "h", "i");
}
|
Then I combine the two.
Code:
$array_haystack = array_combine($letters, $raw_points);
|
At one point I was trying this but could not get it to display correctly.
Code:
$needle = 1;
$correct_answer = array_keys($array_haystack, $needle);
|
Then I tried something like
Code:while (list($key, $val) = each($array_haystack)) {
if($val == 1) {
$correct_answer = $key;
}
}
|
But still could not get those question with more than one answer to display correctly.
How do I get this working so that I can pass it to my template?
Code:$template -> assign_block_vars('stats', array('QNO' => $stats['ques_order'], 'QTITLE' => $stats['ques_name'], 'QDBNO' => $stats['ques_id'], 'BGCOLOR' => $color,
'WHOLE' => $stats['whole'], 'UPPER' => $stats['upper25'], 'LOWER' => $stats['lower25'],
'DISCRIM' => round((float)($stats['discri']),2),
'CORRECT' => $correct_answer,
'AFREQ' => $freq[0], 'BFREQ' => $freq[1], 'CFREQ' =>$freq[2], 'DFREQ' => $freq[3], 'EFREQ' =>$freq[4], 'FFREQ' =>$freq[5], 'GFREQ' =>$freq[6], 'HFREQ' =>$freq[7], 'IFREQ' =>$freq[8], 'SUM' => $sum
));
|
|
|
|
montego
Site Admin

Joined: Aug 29, 2004
Posts: 9457
Location: Arizona
|
Posted:
Thu Oct 07, 2010 7:54 pm |
|
You are on the right track with the loop (there are multiple ways of doing it, but this while loop is fine too):
Quote: |
while (list($key, $val) = each($array_haystack)) {
if($val == 1) {
$correct_answer = $key;
}
}
|
The problem is that you keep overlaying your $correct_answer variable! Instead, why not create an empty array before the while, and every time you get a hit on the "1", add another array element for the answer letter. Then, outside the while loop, use implode() to join it all back together in a comma separate string?
 |
_________________ Only registered users can see links on this board! Get registered or login!
Only registered users can see links on this board! Get registered or login! |
|