Jump to content

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>";
}


?>

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.