Hi All,
I have several posts here that are resolved and I am starting a new post since this is a different challenge.
In my quiz database I store the answers like this...
answers <-- table name
answer_id------ quiz_id ----- question_id -------- answer ------ correct <-- field names
1----------------1---------------1----------------1--------------0
2----------------1---------------1----------------2--------------1
3----------------1---------------1----------------3--------------0
4----------------1---------------1----------------4--------------0
5----------------1---------------2----------------1--------------0
6----------------1---------------2----------------2--------------0
7----------------1---------------2----------------3--------------1
8----------------1---------------2----------------4--------------0
And I have a function written that compares the answers supplied from my form to an array...
//check the answers and also see if they missed any questions
$a=0;
$ar=0;
//$ans = Total questions answered
$ans = 0;
for ($k=1; $k<=$num_questions; $k++) {
$answers = array(NULL,'C', 'D', 'D', 'C', 'A', 'B', 'A', 'D', 'B', 'C', 'C', 'C', 'B'); //this is the correct answers, the null is because arrays start at 0
if(@$_POST['q'.$k] == $answers[$k]){ //check each answer against its value in the array
$a++;
} else {
//continue
}
$question = $k; //assign each value in the array to avariable
if(!isset($_POST['q'.$k])){
echo "<br><font face='Arial' size='2' color='#FF0000'>Question: <b>" . $k . "</font></b> was not answered. <a href='javascript:history.go(-1)'>Please go back and answer all the questions</a>";
}else{
echo "<br><font face='Arial' size='2' color='#FF0000'>Question: <b>" . $k . '</font></b>. You answered ' . @$_POST['q'.$k] . ', which was ' . ((@$_POST['q'.$k] == $answers[$k])?'correct':'incorrect');
$ans++;
}
$ar++;
}
and what I am trying to do is get the
$answers = array(NULL,'C', 'D', 'D', 'C', 'A', 'B', 'A', 'D', 'B', 'C', 'C', 'C', 'B'); //this is the correct answers, the null is because arrays start at 0
data from my table instead of the 'hard coded' data.
I tried to apply the suggestions from another post...
$query="
SELECT question_id, correct
FROM answers
WHERE quiz_id='1'
order by question_id, correct";
$result = mysql_query($query) or die(mysql_error());
if($result)
{
// make an array of alphabet keys to be used in answer array
$AlphKeys=array('A','B','C','D');
while($row = mysql_fetch_array($result))
{
// count the number of answers already processed with the same 'question_id'
$count = count ( $answer[$row['question_id']]);
// use the count to set a new alphabet key from the $AlphaKeys array
$answer[$row['question_id']][$AlphKeys[$count]] = stripslashes($row['correct']);
}
}
But that does not seem to get me what I need.
I am still trying to wrap my brain around multi dim arrays.
I really need some guidance/advise here.
Thanks again,
Don