Jump to content

How to display number of correct answers


MjM8082

Recommended Posts

On my application there is 10 math questions. What I want to do when the test is finished is for it to tell the user how many they got right out of 10. Also how to put this into the database. I have everything else working fine on this application, I just would like to further enhance it by doing this.

 

 

 

This is the code to my application. It is a small application.

 

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiplication Test</title>
</head>

<body>

   

    </body>


<?php

	require_once('database.php');
	session_start();






define ('ROWS', 2);
define ('COLS', 5);
define ('MAX_NUMBER', 12);
date_default_timezone_set('America/New_York');



if (isset($_POST['btn_score']))
{

	$result_name= $_POST['result_name'];

	$correct = 0;


	//print_r ($_POST);
	$time1 = $_POST['ts'];
	$time1_object = new DateTime($time1);
	$now = new DateTime();
	$time_span = $now->diff($time1_object);

	$minutes = $time_span->format('%i');
	$seconds = $time_span->format('%s');
	$seconds+= $minutes * 60;

	echo "It took $seconds seconds to complete the test<hr />";



	foreach ($_POST as $problem => $answer)
	{
		if ($problem <> "btn_score" && $problem <> "ts" && $problem <> "result_name")
		{
			//echo "$problem -- $answer <br />";
			$problem = explode('_', $problem);
			$num1 = $problem[2];
			$num2 = $problem[3];



			$right = $num1 * $num2;


			if ($answer != $right)
			{
			echo "$num1 * $num2 = $answer , The right answer is $right<br />";
			}else 
			{
				$correct = $correct + 1;

			}




			}
	}
	$result_score= 0;
	$result_score= ($correct / 10) * 100;

	echo "your score is <br/>$result_score<br/>";

}






{


	$sql = "INSERT INTO results (result_name, result_score, result_date_time) VALUES ('$result_name','$result_score', NOW());";
	$db->exec($sql);

}



	$query		= "SELECT * FROM results WHERE result_name = :result_name ";

	$statement	= $db->prepare($query);
	$statement->bindValue (':result_name', $result_name);
	$statement->execute();
	$results	= $statement->fetchAll();
	$statement->closeCursor();

	echo "<h1>Show Grades for $result_name </h1>";



	foreach ($results as $result)
	{

	echo $result['result_name'] . " " . $result['result_score']. " " . $result['result_date_time'];	
	echo '<br />';  
	}











?>

<h1>Multiplication Test</h1>

<form name="lab5" method="post" action="lab5b.php">
<?php





	$now = new DateTime();
	//echo $now->format('Y-m-d H:i:s');
	echo "<input type='hidden' name='ts' value='" . $now->format('Y-m-d H:i:s') . "'>";
?>
<table border="1" cellspacing="5" cellpadding="5">

<?php


	$no_of_problems = 0;
	for ($row=0; $row<ROWS; $row++)
	{
		echo "<tr>";
		for ($col=0; $col<COLS; $col++)
		{
			$num1 = mt_rand(1,MAX_NUMBER);
			$num2 = mt_rand(1,MAX_NUMBER);

			echo "<td>$num1 * $num2 </td>";
			echo "<td><input type='text' size='2' name=${no_of_problems}_mult_${num1}_${num2}></td>";
			$no_of_problems++;
		}
		echo "</tr>";
	}
	$colspan = 2 * COLS;
	echo "<tr><td colspan=$colspan align='right'><input type='submit' value='Score' name='btn_score'></td></tr>";
?>	

</table> 

<br>
<br>

<label for="result_name">Student Name:</label> 
<input type="text" id="result_name" name="result_name" /><br />	
</form>
<br>
<br>





</body>
</html>

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.