Ravens PHP Scripts: Forums
 

 

View next topic
View previous topic
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP
Author Message
Donovan
Client



Joined: Oct 07, 2003
Posts: 735
Location: Ohio

PostPosted: Mon Oct 04, 2010 9:32 am Reply with quote

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
                        ));
 
View user's profile Send private message Visit poster's website ICQ Number
montego
Site Admin



Joined: Aug 29, 2004
Posts: 9457
Location: Arizona

PostPosted: Thu Oct 07, 2010 7:54 pm Reply with quote

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?

Wink

_________________
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! 
View user's profile Send private message Visit poster's website
Donovan







PostPosted: Fri Oct 08, 2010 7:38 am Reply with quote

So you think something like this?

Code:
$array_haystack = array_combine($letters, $raw_points);

      
reset($array_haystack);
unset($correct_answer);
unset($key);

$correct_answer = array();
while (list($key, $val) = each($array_haystack)) {
   
   if($val == 1) {   
   array_push($correct_answer, $key);    
   }     
}
$correct_answers = explode(",", $correct_answer); 
 
montego







PostPosted: Sat Oct 09, 2010 12:29 pm Reply with quote

Well, you don't have to use array_push(), a simple $correct_answer[] = $key would have worked too. Wink

But your last line there, I said you needed to use implode() not explode().
 
Display posts from previous:       
Post new topic   Reply to topic    Ravens PHP Scripts And Web Hosting Forum Index -> PHP

View next topic
View previous topic
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB © 2001-2007 phpBB Group
All times are GMT - 6 Hours
 
Forums ©