Jump to content

Calculate Score of Test


MjM8082

Recommended Posts

I can't seem to figure out how to calculate the score the user gets when they hit the score button.

 

I figured it would look something like this...

 

$result_score = $right / 9 * 100.... but I've tried a few methods and I can't seem to figure out why its not working.

 

 

Here is my code for the 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>

<?php

	require_once('database.php');






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



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

	$result_name= $_POST['result_name'];
	$result_score= $_POST['result_score'];

	$result_score = / 9 * 100;



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


	$db->exec($sql);







	//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")
		{
			//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 />";
			}








		}
	}
}









?>

<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

Not working as in?

 

We can't test the code ourselves, considering we don't have the includes or database.

 

Have you tried isolating or simplifying the issue? Perhaps only writing the bare code needed to echo your expect results?

Link to comment
Share on other sites

<!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>

<?php

	require_once('database.php');






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



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

	$result_name= $_POST['result_name'];
	$result_score= $_POST['result_score'];

	$result_score /= 9 * 100;



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


	$db->exec($sql);







	//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")
		{
			//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 />";
			}








		}
	}
}









?>

<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>

 

Changed line 32 and fixed your br's near the bottom.  May possibly fix the issue.

Link to comment
Share on other sites

You don't  need the database to test the code.. All I'm trying to figure out right now is how to calculate a score for the user. There is 9 multiplication problems the user will have to solve and the score is out of 100. So I need to figure out how many they entered correctly and divide it by 9 then times it by 100. But I'm stuck on the first part and don't know how to determine how many problems the user got correct.

Link to comment
Share on other sites

So we have to modify your code just to test it? If you want more people to help you, don't post a wall of code. Cut it down to what's needed, and let us know exactly what isn't working.

 

Anyways, here's an example of a stripped down, working version of what you want to do. With comments, cause I'm a sweetheart. Let me know if you have any problems.

 

<?php 

$questions = 9;

// Use sessions to pass answers to next page. This will prevent manipulation
session_start();

// Check if form has been submitted, and that the token exists
if( isset($_POST['token']) && isset( $_SESSION['math'][$_POST['token']] ) ) {
$score = 0;
for( $i = 0; $i < $questions; $i++ ) {
	echo makeResponse( $i+1, $_POST['q'][$i], $_SESSION['math'][$_POST['token']][$i] );
	// Tally a score - if the question matches what it should be
	if( $_POST['q'][$i] == $_SESSION['math'][$_POST['token']][$i] )
		// Add 10 to score
		$score += 10;
}
echo makeParagraph( 'Your score was '.$score.'/'.(10*$questions) );
// Once answers have been checked, clear the session
unset( $_SESSION['math'][$_POST['token']] );
// Otherwise, no questions have been submitted, so we'll generate
} else {
// Create a unique token to track this specific test
$token = uniqid(rand(0,100));
// Create random questions with matching answers
for( $i = 0; $i < $questions; $i++ ) {
	$question[$i] = array( rand(2,12),rand(2,12) );
	$_SESSION['math'][$token][$i] = $question[$i][0] * $question[$i][1];
}
echo makeForm( $question, $token );
}


// This is to keep the HTML and code separate
function makeForm( $questions, $token ) {
$return = '<form method="post" action="#"><input type="hidden" name="token" value="'.$token.'">';
foreach( $questions as $k => $q ) {
	$return .= '<label for="q_'.$k.'">'.$q[0].' x '.$q[1].': </label>';
	$return .= '<input size="4" type="text" name="q['.$k.']" id="q_'.$k.'"><br>';
}
return $return . '<input type="submit"></form>';
}

function makeResponse( $number, $guess, $answer ) {
$return = '<p>'.$number.': ';
if( $guess == $answer )
	$return .= '<span style="color:green;">Correct</span> The answer was '.$answer;
else
	$return .= '<span style="color:red;">Wrong</span> You guessed '.$guess.
		'. The correct answer was '.$answer;
return $return .'</p>';
}

function makeParagraph( $content ) {
return '<p>'.$content.'</p>';
}

?>

 

Notice how I separate markup and code. This is a great practice, and leads to quicker debugging and understanding. Posting your code with markup intertwined with code confuses and frustrates us. Follow my advice and I promise you will get quicker, more accurate help with your problems.

Link to comment
Share on other sites

You can get percentage with this function

 

function percent($score,$totNum)
{
if(is_numeric($score) && is_numeric($totNum))
{

return ($score / $totNum) * 100;

}
else
{

return 0;

}

 

You can create a function to handle the subtraction as well as returning the percent and score. Make sur ethe percent function is one the same page or included and you'll get an array with the keys score and percent.

 

function score($totNum,$numMissed,$retPer=0)
{


if(is_numeric($totNum) && is_numeric($numMissed))
{

$score = $totNum - $numMissed;
if($retPer == 0)
{

return $score;

}
else
{

$per = percent($score,$totNum);
$scores = array("score" => $score,"percent" => $per);
return $scores;
}

}

}

 

As far as checking scores you need to compare answers by comparing the answers to a dictionary of answers for that question/list of questions. How you implement this is up to you but most just make two arrays and compare each ones values.

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.