first time using arrays
Posted: Mon Dec 05, 2005 3:26 am
Hi everyone
This is my first time using arrays. I am creating a quiz and putting the answers into a $_SESSION['answers_array'];
I am having problems with the following code. It is setting values to:
and
but not to:
or
I have posted the full code below please can you help!
(Please ignore the echo ""; as these are part of the debug check)
This is my first time using arrays. I am creating a quiz and putting the answers into a $_SESSION['answers_array'];
I am having problems with the following code. It is setting values to:
Code: Select all
$answers_array['quiz_id']Code: Select all
$answers_array['quiz_id']['question_number']Code: Select all
$answers_array['quiz_id']['question_number']['optionid']Code: Select all
$answers_array['quiz_id']['question_number']['optionid']['answer'](Please ignore the echo ""; as these are part of the debug check)
Code: Select all
require_once('../../php/page_functions.php');
require_once('../../Connections/db_connect.php');
if(empty($_POST['quiz_id']) or empty($_SESSION['question_number'])) { header("Location:index.php"); }
$quiz_id = $_POST['quiz_id'];
$question_number = $_SESSION['question_number'];
// seting vars to 0
$total_sent_answers = '0';
// database queries
mysql_select_db($database_db_connect, $db_connect);
$query_quiz_top = "SELECT * FROM quizzes_top WHERE id = '$quiz_id' ORDER BY id DESC";
$quiz_top = mysql_query($query_quiz_top, $db_connect) or die(mysql_error());
$row_quiz_top = mysql_fetch_assoc($quiz_top);
$totalRows_quiz_top = mysql_num_rows($quiz_top);
mysql_select_db($database_db_connect, $db_connect);
$query_question = "SELECT * FROM quizzes WHERE question_number = '$question_number' AND quiz_id = '$quiz_id' ORDER BY id ASC";
$question = mysql_query($query_question, $db_connect) or die(mysql_error());
$row_question = mysql_fetch_assoc($question);
$totalRows_question = mysql_num_rows($question);
mysql_select_db($database_db_connect, $db_connect);
$query_options = "SELECT * FROM quiz_options WHERE question_id = '$question_number' AND quiz_id = '$quiz_id' ORDER BY id ASC";
$options = mysql_query($query_options, $db_connect) or die(mysql_error());
$row_options = mysql_fetch_assoc($options);
$totalRows_options = mysql_num_rows($options);
mysql_select_db($database_db_connect, $db_connect);
$query_options_correct = "SELECT * FROM quiz_options WHERE question_id = '$question_number' AND quiz_id = '$quiz_id' AND correct = 'Y' ORDER BY id ASC";
$options_correct = mysql_query($query_options_correct, $db_connect) or die(mysql_error());
$row_options_correct = mysql_fetch_assoc($options_correct);
$totalRows_options_correct = mysql_num_rows($options_correct);
// setting array
if(!isset($answers_array)){ $answers_array = array(); }
//setting array vars (3d array)
$answers_array[$quiz_id] = $quiz_id;
echo "quiz_id :".$answers_array[$quiz_id]."<br>";
$answers_array[$quiz_id][$question_number] = $question_number;
echo "question number: ".$answers_array[$quiz_id][$question_number]."<br>";
//checking vars and putting into array
do{
$optionid = $row_options['id'];
echo $optionid;
$answers_array[$quiz_id][$question_number][$optionid] = $optionid;
echo "option id: ".$answers_array[$quiz_id][$question_number][$optionid]."<br>";
$answervar = "option".$optionid;
$answer = $_POST[$answervar];
// set answer to no if not Yes
if ($answer <> 'Y') { $answer = 'N';
} else {
$total_sent_answers++;
}
$answers_array[$quiz_id][$question_number][$optionid][$answer] = $answer;
session_register($answers_array);
} while ($row_options = mysql_fetch_assoc($options));
// counting correct answers and redirecting if no correct amount
if($total_sent_answers <> $totalRows_options_correct) {
//echo "<br>sent answers: $total_sent_answers";
//echo "<br>total correct answers: $totalRows_options_correct";
//echo "<br>question number: $question_number";
header("Location:take_quiz.php?quiz_id=".$quiz_id."&error=options");
}
if($total_sent_answers == $totalRows_options_correct) {
//echo "<br>sent answers: $total_sent_answers";
//echo "<br>total correct answers: $totalRows_options_correct";
//echo "<br>question number: $question_number";
$question_number++;
session_register($question_number);
if($question_number > $row_quiz_top['no_of_questions']) {
// collecting information
$username = $_SESSION['MM_Username'];
$score = base64_encode(serialize($answers_array));
$date = date('d/m/Y');
//inserting data ito database
mysql_select_db($database_db_connect, $db_connect);
$insert_sql = "INSERT INTO quiz_scores(username,score,date,quiz_id)
VALUES('$username','$score','$date','$quiz_id')";
mysql_query($insert_sql, $db_connect) or die(mysql_error());
header("Location:results.php?quiz_id=$quiz_id");
} else {
header("Location:take_quiz.php?quiz_id=$quiz_id");
}
}