Jump to content

code correct but not working together!


Doug

Recommended Posts

hello,

 

I am trying to build a quiz for my website.

 

I have the code below that works fine.

 

All of the questions list at the top with correct answer below each question

 

Under this all of the users answers are displayed (the option they took eg 'C') with the correct option.

 

What i am trying to achieve is to join the two. So that you get Question text, Text answer, option user took and correct option for each quiz question.

 

Help much appreciated.

 


<?php   
$total=0;
$t = 0;

require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT * FROM questions';
$result = mysqli_query($dbc, $query);

// if records are present 
if (mysqli_num_rows($result) > 0) { 



// print answer list as check boxes 
while ($row = mysqli_fetch_array($result)) { 

$id = $row['autokey'];
$tanswer = $row['text_answer'];
$quest = $row['q_text'];
$answer= $row['answer'];

echo "<p><strong>Question{$id}:</strong> {$quest}"; ?> <br />
<?php
echo "  answer: {$tanswer}";
?> <br />
<?php

}}

$query = 'SELECT * FROM questions';
$result = mysqli_query($dbc, $query);

while ($row = mysqli_fetch_assoc($result))
{    	

	$question_id = $row['autokey'];    	
	$answer = $row['answer'];  	
	$questiontext = $row['q_text'];
	$question_answers[$question_id] = $answer;

}

// loop through the posted answers and compare

foreach($question_answers as $question_id   => $question_answer)

{

if(isset($_POST['question'][$question_id]))      
     {
	$user_answer = $_POST['question'][$question_id];     	     
	if($user_answer == $question_answer)        	     
	    {        		
		     
		echo "<p>Your answer was <strong>correct</strong> for Q{$question_id}"; ?> <br />
<?php      

$total++;
$t++;  	     
	}        	     
	else        	     
	{          		     echo "<p>Your answer was <Strong> incorrect</strong> for Q{$question_id}."; ?> <br />
<?php                 
$t++;  	     
	}           
	echo"You answered: {$user_answer}";

}      
 else    	
	{        			
	echo "<p>You didn't give an answer for Q{$question_id}.";   
$t++;	
	}

	?><br /> <?php

	echo "Correct answer: {$question_answer}<br />";

}

echo "<p>You got {$total} out of {$t} correct.</p>";



?>

Link to comment
https://forums.phpfreaks.com/topic/240363-code-correct-but-not-working-together/
Share on other sites

Is this what you were looking for?

<?php   
require_once('connectvars1.php');

//Connect to the database
        $dbc = mysqli_connect(DB_Host, DB_User, DB_Password, DB_Name);

// get all answers and add them to an array
$query = 'SELECT * FROM questions';
$result = mysqli_query($dbc, $query);
$total = 0;
$num = 0;
// if records are present 
if (mysqli_num_rows($result) > 0) { 

// print answer list as check boxes 
while ($row = mysqli_fetch_array($result)) { 
++$num;
$id = $row['autokey'];
$tanswer = $row['text_answer'];
$quest = $row['q_text'];
$answer= $row['answer'];

echo "<p><strong>Question{$id}:</strong> {$quest}<br />";

	if(!empty($_POST['question'][$id])) {
		if($_POST['question'][$id] == $tanswer) {
			echo "<p>Your answer was <strong>correct</strong> for Q{$question_id}<br />\n";
			++$total;
		}
		else {
			 echo "<p>Your answer was <Strong> incorrect</strong> for Q{$question_id}.<br />\n";
		}
		echo"You answered: " . htmlentities($_POST['question'][$id]) . "<br />\n";
	}
	else {
		echo "<p>You didn't give an answer for Q{$question_id}.";
	}
	echo "Correct answer: {$tanswer}<br /><br />";	
}
echo "<p>You got {$total} out of {$num} correct.</p>";
}


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.