Jump to content

score keeps incrementing? cant see why.


lanceox

Recommended Posts

Hey guys,  I have set a quiz up that pulls questions and answers from an array, every correct answer,.... score increments; however it is within a loop.  Something must be wrong with the loop as it keep incrementing however i cant see why.

Any help would be great,

 

The main aim what i am trying to do is have 5 questions answered and go to different page (different topic for 5 more questions) however keep the on going score.

 

Hope this makes sense. If not ill try and clear it up.

 

Thanks for all your help guys in advance, i appreciate it

 

also not, i have a functions.php, questionsandanswers.php



<?php
session_name("Question_Test");
session_start();
echo $_SESSION['score'];
require_once('questionsandanswers.php');
require_once('functions.php');



if (!isset($_POST['submit'])) 
{
	$score = 0;
	$_SESSION['score'] = $score;
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array();
	$_SESSION['finished'] = 'no';

	if (isset($_SESSION['error']))
	{
		unset($_SESSION['error']); 
		$num = 0;
	} 
		else
	{
	$score = 0;
	$_SESSION['score'] = $score;
	$_SESSION['correct'] = array(); 
	$_SESSION['wrong'] = array(); 
	$_SESSION['finished'] = 'no';
	$num = 0;
	} 
}
else
{
$num = (int) $_POST['num'];
$postedanswers = str_replace("_"," ",$_POST['answers']);
	if ($postedanswers == $answers[$num]['0'])
	{
		$_SESSION['score'] ++;
		$_SESSION['correct'][] = $postedanswers; 
	} 
	else
	{
		$_SESSION['wrong'][] = $postedanswers;
	}	

if ($num < count($questions)-1) 
{
	$num++;
} 
	else 
{
	$last = true;
	$_SESSION['finished'] = 'yes';
}
}

?>
<!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>running an array</title>
<?php 
if (!isset($last)) {
echo "<script type=\"text/javascript\" src=\"form.js\"></script>";
}
?>
</head>
<body>
<h1>Protection Section</h1>
<?php if (!isset($last)){?>
<h2>Questions <?php echo $num+1; ?>:</h2>
<p><strong><?php echo $questions[$num]; ?></strong></p>
<form id="questionBox" method="post" action="test1.php">

<?php
$pattern = ' ';
$replace = '_';
$shuffledAnswers = shuffle_assoc($answers[$num]);
#var_dump($newanswers);
foreach ($shuffledAnswers as $answer) 
		{
			$answer2 = str_replace($pattern,$replace,$answer);
			echo "<li><input type=\"radio\" id=\"$answer2\" value=\"$answer2\" name=\"answers\" />\n";
			echo "<label for=\"$answer2\">$answer</label></li>\n";
		}	
?>
<input type="hidden" name="num" value="<?php echo $num; ?>" />
<input type="submit" id="submit" name="submit" value="Submit Answer" /></p>
</form>
<?php
} else
{ 
	 echo "your final score is:</h2>\n
	 <h3>{$_SESSION['score']}/20</h3><h4>Verdict:</h4>";
	 if($_SESSION['score'] <= 5) echo "<p id=\"verdict\"><span>S</span>everely <span>H</span>indered <span>I</span>n the <span>T</span>est!</p>\n";
	 if(($_SESSION['score'] > 5) && ($_SESSION['score'] <= 10)) echo "<p id=\"verdict\"><span>C</span>ould <span>R</span>ead <span>A</span>nd <span>P</span>ractice more.</p>\n";
	 if(($_SESSION['score'] > 10) && ($_SESSION['score'] <= 15)) echo "<p id=\"verdict\"><span>A</span>cronyms a<span>R</span>e <span>S</span>o <span>E</span>asy!</p>\n";
	 if($_SESSION['score'] > 15) echo "<p id=\"verdict\"><span>S</span>uper <span>A</span>cronym <span>S</span>pecialist</p>";

	 echo "<p id=\"compare\"><a href=\"test2.php\">Next Section! <img src=\"images/arrow.png\" /></a></p>";
}
?>

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