Jump to content

Can't Figure This Out


kwdelre

Recommended Posts

The page that I'm working on is a quiz results page. In this section of code I am simply printing the question, then each answer choice is printed depending on a few if statements. Let's say the answer choice in this first statement was "Baseball." The four possibilities depending on the student answer I want are:

 

Baseball - You Got This Right  or

Baseball - Correct Answer    or

Baseball - Your Answer    or

Baseball

 

So if the student gets a question wrong one choice should say "Correct Answer" next to it and one other would have "Your Answer" next to it. Pretty simple and straightforward.

 

I can get the correct results from everything but the second ifelse statement (Baseball - Your Answer). No matter what i do I can't get this to work.

 

Here is the code below and some clarifications:

 

$StudAns = Student's answer

$Choices = The particular answer choice being evaluated

$CorrectAnswer = obvious

 

 

$StudAns = array($QChoices['Q1Choice'], $QChoices['Q2Choice'], $QChoices['Q3Choice']);

$ContentsQuery = mysql_query("SELECT Question, Choice1, Choice2, Choice3, Correct_Answer FROM Module_1_Quiz")
or die(mysql_error());


while ($Contents = mysql_fetch_array($ContentsQuery)) {

							    $i =0;
								$Choices = array($Contents['Choice1'], $Contents['Choice2'], $Contents['Choice3']);
								$CorrectAnswer = $Contents['Correct_Answer'];



								echo "<span id='answers'><table width = '600' border='0' align='center' id='answers' bgcolor='#FFFFCC'><tr><td>";
								echo $Contents['Question'];
								echo "</td></tr>";
								echo "<tr><td><ol type = 'a'><li>";

								// Checks what should be printed for the first answer option
								if ($Choices[$i] == $CorrectAnswer && $StudAns[$i] == $CorrectAnswer) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($Choices[$i] == $CorrectAnswer &&  $StudAns[$i] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($StudAns[$i] != $CorrectAnswer && $CorrectAnswer[$i] != $Choices[$i]) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}



  									echo "</li><li>";
								$i++;


								// Checks what should be printed for the second answer option
								if ($Choices[$i] == $CorrectAnswer && $StudAns[$i] == $CorrectAnswer) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($Choices[$i] == $CorrectAnswer &&  $StudAns[$i] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($StudAns[$i] != $CorrectAnswer && $CorrectAnswer[$i] != $Choices[$i]) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}




								echo "</li><li>";

								$i++;

								// Checks what should be printed for the third answer option
								if ($Choices[$i] == $CorrectAnswer && $StudAns[$i] == $CorrectAnswer) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($Choices[$i] == $CorrectAnswer &&  $StudAns[$i] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($StudAns[$i] != $CorrectAnswer && $CorrectAnswer[$i] != $Choices[$i]) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}


								echo "</li></ol></table></span>";



								}

 

Thanks everyone!

Link to comment
Share on other sites

Got it

 

$ii=0;

while (
   $Contents = mysql_fetch_array($ContentsQuery)) {

							    $i =0;


								$Choices = array($Contents['Choice1'], $Contents['Choice2'], $Contents['Choice3']);
								$CorrectAnswer = $Contents['Correct_Answer'];




								echo "<span id='answers'><table width = '600' border='0' align='center' id='answers' bgcolor='#FFFFCC'><tr><td>";
								echo $Contents['Question'];
								echo "</td></tr>";
								echo "<tr><td><ol type = 'a'><li>";

								// Checks what should be printed for the first answer option
								if ($StudAns[$ii] ==$CorrectAnswer && $CorrectAnswer == $Choices[$i]) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($StudAns[$ii] != $Choice[$i]  && $Choices[$i] == $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($Choices[$i] != $CorrectAnswer && $StudAns[$ii] == $Choices[$i] && $StudAns[$ii] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}					


  									echo "</li><li>";
								$i++;


								// Checks what should be printed for the second answer option
								if ($StudAns[$ii] ==$CorrectAnswer && $CorrectAnswer == $Choices[$i]) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($StudAns[$ii] != $Choice[$i]  && $Choices[$i] == $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($Choices[$i] != $CorrectAnswer && $StudAns[$ii] == $Choices[$i] && $StudAns[$ii] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}										




								echo "</li><li>";

								$i++;


								// Checks what should be printed for the third answer option
								if ($StudAns[$ii] ==$CorrectAnswer && $CorrectAnswer == $Choices[$i]) { 
								echo $Choices[$i];
								echo " - You Got This Right!";
								}
								elseif ($StudAns[$ii] != $Choice[$i]  && $Choices[$i] == $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Correct Answer";
								}
								elseif ($Choices[$i] != $CorrectAnswer && $StudAns[$ii] == $Choices[$i] && $StudAns[$ii] != $CorrectAnswer) {
									echo $Choices[$i];
									echo " - Your answer";
								}
								else {
									echo $Choices[$i];
								}								

								$ii++;

								echo "</li></ol></table></span>";



								}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.