Click to See Complete Forum and Search --> : Now that it's working, help me mangle it :)


Raskii
03-28-2001, 01:59 PM
OK, I have my PHP Quiz working ( http://www.hcweb.net/mikenune/Quiz.php ). Now what I want to do is make it so that you can have questions with multiple answers and short answers too.

Normally, this would be easy. Just change the form.

Well, I'm getting the questions from a different file so I have to worry about determining what type of question it is, how many answers it has, etc...

Right now, the questions file (Questions.txt) is actually a 2 dimentional array:

<?
// This is the file containing all the $questions. The first of the $questions ($question[0][a - d, 5, 6]) is an example, DO NOT CHANGE IT!
$quizName = "Test Quiz"; // The name of the quiz.
*
// This is the example $question (the template)
$question[0][ques] = "The answer is <B>a</B>."; // This is the $question.
$question[0][studentAns] = ""; // This is the student's answer. DO NOT INCLUDE THIS ONE IN HERE!
$question[0][correctAns] = "a"; // This is the correct answer (a, b, c, or d).
$question[0][a] = "a"; // This is the first answer (choice "a").
$question[0][b] = "b"; // This is the second answer (choice "b").
$question[0][c] = "c"; // This is the third answer (choice "c").
$question[0][d] = "d"; // This is the fourth answer (choice "d").
*
// This is where the real $questions start.
$question[1][ques] = "The answer is <B>a</B>.";
$question[1][correctAns] = "a";
$question[1][a] = "a";
$question[1][b] = "b";
$question[1][c] = "c";
$question[1][d] = "d";
*
$question[2][ques] = "The answer is <B>b</B>.";
$question[2][correctAns] = "b";
$question[2][a] = "a";
$question[2][b] = "b";
$question[2][c] = "c";
$question[2][d] = "d";
*
$question[3][ques] = "The answer is <B>c</B>.";
$question[3][correctAns] = "c";
$question[3][a] = "a";
$question[3][b] = "b";
$question[3][c] = "c";
$question[3][d] = "d";
*
$question[4][ques] = "The answer is <B>d</B>.";
$question[4][correctAns] = "d";
$question[4][a] = "a";
$question[4][b] = "b";
$question[4][c] = "c";
$question[4][d] = "d";
*
$question[5][ques] = "The answer is <B>c</B>.";
$question[5][correctAns] = "c";
$question[5][a] = "a";
$question[5][b] = "b";
$question[5][c] = "c";
$question[5][d] = "d";
*
$question[6][ques] = "The answer is <B>a</B>.";
$question[6][correctAns] = "a";
$question[6][a] = "a";
$question[6][b] = "b";
$question[6][c] = "c";
$question[6][d] = "d";
?>

The PHP script that created the form is called Quiz.php:

<?
include("Questions.txt");
?>
<HTML>
<HEAD>
<TITLE> <? print("$quizName"); ?> </TITLE>
</HEAD>
<BODY BGCOLOR="#DDDDDD">
*
<?
// If they've clicked on "Hand in Quiz"
if($answered == 1) {
$numRight = 0; // Initialize the number of correct questions (so if they don't get ANY right, it'll show "0" instead of nothing).
*
// Counting the number of correct questions...
for($count = 1;$count <= (count($question) - 1);$count++) {
if ($question[$count][studentAns] == $question[$count][correctAns]) {
$numRight++; } }
$studentScore = ($numRight / (count($question) - 1)) * 100; // Turning the score into a percentage.
*
// Writing a file that records all the information of the quiz...
$recordFile = "RecordFile.txt";
$myRecordFile = fopen($recordFile,"a+");
fputs($myRecordFile,"IP Address: NOT IMPLIMENTED YET.\n");
fputs($myRecordFile,"Number of questions: (count($question) - 1)\n");
fputs($myRecordFile,"Number of correct answers: $numRight\n");
fputs($myRecordFile,"Score: $studentScore %\n\n");
fclose($myRecordFile);
*
// Printing out the quiz stats...
print("<FONT SIZE=\"+1\"><B>Number of questions:</B></FONT> " . (count($question) - 1) . "<BR>\n");
print("<FONT SIZE=\"+1\"><B>Number of correct answers:</B></FONT> $numRight<BR>\n");
print("<FONT SIZE=\"+1\"><B>Your Score:</B></FONT> ");
*
// Formatting the output of the score (color coding based on the score:
// 90% to 100% - Green text
// 80% to 90% - Plain black text
// 70% to 80% - Yellow text
// less than 70% - Red text)
if($studentScore >= 90 ) {
print("<FONT COLOR=\"#008800\">$studentScore %</FONT><BR>\n");
} else if($studentScore >= 80 && $studentScore < 90) {
print(round($studentScore,2) . " %<BR>\n");
} else if($studentScore >= 70 && $studentScore < 80) {
print("<FONT COLOR=\"#909000\">" . round($studentScore,2) . " %</FONT><BR>\n");
} else {
print("<FONT COLOR=\"#880000\">" . round($studentScore,2) . " %</FONT><BR>\n"); }
*
// Printing out the question, the correct answer, and the student's answer for each question...
print("<BR>\n\n");
for($count = 1;$count <= (count($question) - 1);$count++) {
print("Question No. $count.<BR>\n");
print("Correct Answer: <B>{$question[$count][correctAns]}</B><BR>\n");
if ($question[$count][studentAns] == $question[$count][correctAns]) {
print("Your Answer: <FONT COLOR=\"#008800\">{$question[$count][studentAns]}</FONT><BR>\n<BR>\n\n");
} else {
print("Your Answer: <FONT COLOR=\"#880000\">{$question[$count][studentAns]}</FONT><BR>\n<BR>\n\n"); } }
*
// If they haven't taken the quiz...
} else {
print("<FORM ACTION=\"Quiz.php\" METHOD=\"post\">\n\n");
*
// Printing out the question and all the possible answers...
for($count = 1;$count <= (count($question) - 1);$count++) {
print("<FONT SIZE=\"+1\"><B>Question No. $count</B></FONT><BR>\n");
print("{$question[$count][ques]}<BR>\n");
print("<INPUT TYPE=\"radio\" NAME=\"question[$count][studentAns]\" VALUE=\"a\"> <B>a.</B> {$question[$count][a]}<BR>\n");
print("<INPUT TYPE=\"radio\" NAME=\"question[$count][studentAns]\" VALUE=\"b\"> <B>b.</B> {$question[$count][b]}<BR>\n");
print("<INPUT TYPE=\"radio\" NAME=\"question[$count][studentAns]\" VALUE=\"c\"> <B>c.</B> {$question[$count][c]}<BR>\n");
print("<INPUT TYPE=\"radio\" NAME=\"question[$count][studentAns]\" VALUE=\"d\"> <B>d.</B> {$question[$count][d]}<BR>\n");
print("<BR>\n\n"); }
*
// Printing out the closing tags...
print("<INPUT TYPE=\"hidden\" NAME=\"answered\" VALUE=\"1\">\n");
print("<INPUT TYPE=\"submit\" VALUE=\"Hand in Quiz\"> * * * * * <INPUT TYPE=\"reset\" VALUE=\"Retake Quiz\">\n");
print("</FORM>\n"); }
?>
*
</BODY>
</HTML>


How can I change these to allow multiple answer and short answer questions?

EDIT:Wow, no blank lines... Better put those in. Yes, I know that my formatting's a little off. I'm fixing that too

[ 28 March 2001: Message edited by: Lucefiel ]