MAJOR HELP PLEASE!!!

I am doing this project for a class that involves PHP and I’m off to a good start. What I’m trying to accomplish is create a webpage that is based off the game Mad Gab (here’s a reference for it: http://en.wikipedia.org/wiki/Mad_gab). My webpage gives the user five problems to try and figure out and forms for typing in their answer. Once their finished they hit the “submit” button and ideally are brought to a page that displays the following in this order: a) the problem as it was presented b) their answer as typed in the forms c) the correct answer. Now the tricky part is that the first page (aka the page where they guess) is randomly generated each time the page is loaded. Each problem is configured to randomly pick from a possible 4 questions. That part I have figured out. The problem is, when the user gets to the answer page, I want the question to be displayed with its corresponding answer (not just a random answer). I tried using a “Switch” statement and cases to try solve this problem, but for some reason it’s not working. Also, the question and answers are written as pure text files and are read using the “Include” statement. What I have figured out is how to randomly generate questions when the user enters the page and I kind of have an idea that when the user hits the submit button, the questions that were choosen need to be sent with as well, so the page knows which answers to display.
For instance:
Question 1
here i am

"submit"

--------(answer page)------

Question 1 - this is named something like Q1 and I believe is sent as hidden input
Your Answer
The Correct Answer - is a switch statement saying that if Q1=1 display this file Q1ans, Q1=2 display Q2ans, etc

Here is the webpage as it stands now:
http://cordova.asap.um.maine.edu:16080/ … onpart.php

Here’s my code for the question page**********

[code]

<head>
	<title>

Jeremy’s Mad Gab!



Are you ready to play Mad Gab?

Directions: Open the Max/MSP program titled "Audio Talker" and in that program lower the "sensitivity" to 1. Then, size both this window and the "Audio Talker" windows so they sit side by side on your computer screen. Now, click the speaker button in the "Audio Talker" and say one of the phrases listed below. Try to figure out what is actually being said by listening to the playback of your own voice. Type in your answer below the problem and proceed to the next problem and repeat the steps above until your finished all of the problems. Then click the "Submit" button at the bottom of the screen.



1)

<? $Q1 = rand(1,4); include ('A'.$Q1.'.txt'); ?>


2)

<? $Q2 = rand(1,4); include ('B'.$Q2.'.txt'); ?>


3)

<? $Q3 = rand(1,4); include ('C'.$Q3.'.txt'); ?>


4)

<? $Q4 = rand(1,4); include ('D'.$Q4.'.txt'); ?>


5)

<? $Q5 = rand(1,4); include ('E'.$Q5.'.txt'); ?>




</body>
[/code]

Here’s the code for the answer page

[code]

<head>
	<title>Your Answers</title>
</head>
<body>
	<CENTER><H1>Your Answers</H1></CENTER>
<?php

 $Answer1 = $_POST['FirstAnswer'];
	Switch ($Q1) {
		case $Q1=1:
			$text = file_get_contents("A1ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q1=2:
			$text = file_get_contents("A2ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q1=3:
			$text = file_get_contents("A3ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q1=4:
			$text = file_get_contents("A4ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;

}
echo $Answer1;
echo “

”;

$Answer2 = $_POST['SecondAnswer'];
	Switch ($Q2) {
		case $Q2=="1":
			$text = file_get_contents("B1ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q2=="2":
			$text = file_get_contents("B2ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q2=="3":
			$text = file_get_contents("B3ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q2=="4":
			$text = file_get_contents("B4ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;

}
echo $Answer2;
echo “

”;

$Answer3 = $_POST['ThirdAnswer'];
	Switch ($Q3) {
		case $Q3=="1":
			$text = file_get_contents("C1ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q3=="2":
			$text = file_get_contents("C2ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q3=="3":
			$text = file_get_contents("C3ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q3=="4":
			$text = file_get_contents("C4ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;

}
echo $Answer3;
echo “

”;

$Answer4 = $_POST['FourthAnswer'];
	Switch ($Q4) {
		case $Q4=="1":
			$text = file_get_contents("D1ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q4=="2":
			$text = file_get_contents("D2ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q4=="3":
			$text = file_get_contents("D3ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q4=="4":
			$text = file_get_contents("D4ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;

}
echo $Answer4;
echo “

”;

$Answer5 = $_POST['FifthAnswer'];
	Switch ($Q5) {
		case $Q5=="1":
			$text = file_get_contents("E1ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q5=="2":
			$text = file_get_contents("E2ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q5=="3":
			$text = file_get_contents("E3ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;
		case $Q5=="4":
			$text = file_get_contents("E4ans.txt");
			$br_text = str_replace("/n", "<BR>", $text);
			echo $br_text;
			break;

}
echo $Answer5;
echo “

”;

?>



</body>
[/code]

MOD EDIT: Added code tags

First thing I was thinking was: dear god, you need loops. Your current code is WAAAY too long since you’re doing repetitive things.
See the below code, and pay special attention to the comments:

[code]

Jeremy's Mad Gab!

Are you ready to play Mad Gab?

Directions: blabla yourtexthere



<?php // ALL QUESTIONS IN ONE FILE - ONE QUESTION PER LINE $questions = file("questions.txt"); // KEEP TRACK OF WHICH QUESTIONS ARE ALREADY DISPLAYED $currentquestions = 0; // 5 QUESTIONS PER VISITOR for ($i = 1; $i <= 5; $i++) { // PICK A RANDOM QUESTION $question_number = rand(1, count($questions)); // HAS THIS QUESTION BEEN USED ALREADY? if (!in_array($question_number, $currentquestions)) { // IF NOT, ADD IT TO THE USED QUESTIONS $currentquestions[$i] = $question_number; // OUTPUT THE QUESTION AND THE ANSWER TEXTBOX echo "

".$i.")

".$questions[$question_number]; echo "


"; } else { // IF THE QUESTION HAS ALREADY BEEN ASKED, MAKE ANOTHER LOOP ITERATION $i--; } } ?> [/code]

The same thing can be done with your answers page, since there too, you’re doing a lot of repetitive work. Loops will make your code easier to maintain, debug and enhance :)

I understand how that works for the question page, but for the answer page, each question has a specific answer to it. Should I use an “if” statement or is there another way?

switch statements are your friend:

switch($task)
{
	case 'getCells':
		$returnValue = getAvailableCells($client);
		break;
	case 'getShells':
		$returnValue = getAvailableShells($client, $_GET['cell']);
		break;
	case 'createUserID':
		$returnValue = createUserID($client);
		break;
}

each case statement compares the values in single quotes to the value in $task

Sponsor our Newsletter | Privacy Policy | Terms of Service