PDA

View Full Version : Help with maths game scoring


MattK12
11-04-2004, 12:04 PM
Hi im new to php and i was wondering if somebody could help me with some code, ive coded a subtraction type game (text based), and now im trying to implement a score, but alas everything i do results in a score have a maximum of 1 and a minimum of 0. Here is my current code and at the moment it probably has some uneeded html or php can someone give me advice on how i can make a scoring system that actually works. If possible how would i be able to have two sets of questions, i just seem to copy my previous code wrong and cannot get it to comply to my needs.

Note: If the answer is correct i want one point added to the score but if the answer is wrong i want their score to remain the same.

Regards Matthew

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #FFFFFF;
text-decoration: blink;
}
body {
background-image: url(h:\My Pictures\grass.jpg);
}
.style2 {
color: #FFFFFF;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.style6 {font-size: 12px}
-->
</style>
</head>

<body bgcolor="#000000"><html>
<head>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center">&nbsp;</p>
<title>
<div align="center" class="style1">SUBTRACTION PAGE </div>
</title>
<div align="center"> </head>
<p>
<script type="text/javascript" src="file:///E|/scripts.js"></script>
<script language="JavaScript" type="text/JavaScript">
adjust_popup();
</script><body>
<? $score = 0?>
<?
$first = rand(11, 25);
$second = rand(1, 10);


if(isset($_POST['mSubmit'])) {
if(($_POST['first'] - $_POST['second']) == $_POST['answer']) {
echo '<font color= "white"> Correct! </font>';
$score ++;
echo $score;
} else {
echo '<font color= "white"> Wrong! </font>';
$first = $_POST['first'];
$second = $_POST['second'];
}
}
?>
</p>
</div>
<form action="<?=$_SERVER['file:///E|/php_self'];?>" method="post">
<div align="center">
<span class="style1">
<span class="style1">
<input type="hidden" name="first" value="<?=$first;?>">
<input type="hidden" name="second" value="<?=$second;?>">
<?=$first;?>
take
<?=$second;?>
equa</span>ls</span>
<input type="text" name="answer" size="3">
<input type="submit" name="mSubmit" value="Enter">
</div>
</form>
</body>
</html>


</body>
</html>

Badman3k
11-05-2004, 11:22 AM
Okay simple problem, you kept initialising score to 0 so you were overwriting the score each time the user submitted.

So, I've added a new hidden field to store the score, and also add an if(!isset()) line to test to see whether or not this is the first question, and if it is then set the score counter to zero otherwise cause the score's being submitted each time this wont reset it. (Probably a bad explanation :o)


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #FFFFFF;
text-decoration: blink;
}
body {
background-image: url(h:My Picturesgrass.jpg);
}
.style2 {
color: #FFFFFF;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.style6 {font-size: 12px}
-->
</style>
</head>

<body bgcolor="#000000"><html>
<head>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center">&nbsp;</p>
<title>
<div align="center" class="style1">SUBTRACTION PAGE </div>
</title>
<div align="center"> </head>
<p>
<script type="text/javascript" src="file:///E|/scripts.js"></script>
<script language="JavaScript" type="text/JavaScript">
adjust_popup();
</script><body>
<?
$first = rand(11, 25);
$second = rand(1, 10);
if(!isset($_POST['score'])){ $score = 0; }

if(isset($_POST['mSubmit'])) {
if(($_POST['first'] - $_POST['second']) == $_POST['answer']) {
echo '<font color= "white"> Correct! </font>';
$score ++;
echo '<font color= "white">'.$score.'</font>';
} else {
echo '<font color= "white"> Wrong! </font>';
$first = $_POST['first'];
$second = $_POST['second'];
}
}
?>
</p>
</div>
<form action="<?=$_SERVER['file:///E|/php_self'];?>" method="post">
<div align="center">
<span class="style1">
<span class="style1">
<input type="hidden" name="first" value="<?=$first;?>">
<input type="hidden" name="second" value="<?=$second;?>">
<input type="hidden" name="score" value="<?=$score;?>">
<?=$first;?>
take
<?=$second;?>
equa</span>ls</span>
<input type="text" name="answer" size="3">
<input type="submit" name="mSubmit" value="Enter">
</div>
</form>
</body>
</html>


</body>
</html>


That should take care of your problem and appears to work.

MattK12
11-07-2004, 09:40 AM
Hmm i seem to understand what you are saying but the code you have displayed seems to have the same error and im having trouble performing the operation you describe. Am i missing something ?

thanks Matthew

Badman3k
11-07-2004, 04:30 PM
Hmm that's interesting that you're having problems. The only things I've added are the if (!isset($_POST['score'])){ score = 0;} so that it initialises score to 0 on the very first time, and the <input type=hidden value="<?=score;?>"> tag to continue transfering the score. Don't forget to remove your original $score=0; from the code as you'll end up with the same problem.

I've copied the code I posted and I've uploaded it to my webspace, so you can see it working. It's at: http://www.joropo.co.uk/test.php.

If you're still having trouble post your source and I'll have another look at it.

deadimp
11-08-2004, 12:51 AM
You can always try using cookies (http://www.php.net/manual/en/ref.http.php) or session (http://www.php.net/manual/en/ref.session.php) variables instead (maybe too much of a hassle).

MattK12
11-08-2004, 07:45 AM
Ok i thought i had it working but now its doing it all over agin, could it be a problem with foxserv or?

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #FFFFFF;
text-decoration: blink;
}
body {
background-image: url(h:My Picturesgrass.jpg);
}
.style2 {
color: #FFFFFF;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.style6 {font-size: 12px}
-->
</style>
</head>

<body bgcolor="#000000"><html>
<head>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center">&nbsp;</p>
<title>
<div align="center" class="style1">SUBTRACTION PAGE </div>
</title>
<div align="center"> </head>
<p>
<script type="text/javascript" src="file:///E|/scripts.js"></script>
<script language="JavaScript" type="text/JavaScript">
adjust_popup();
</script><body>
<?
$first = rand(11, 25);
$second = rand(1, 10);
if(!isset($_POST['score'])){ $score = 0; }
else{}

if(isset($_POST['mSubmit'])) {
if(($_POST['first'] - $_POST['second']) == $_POST['answer']) {
echo '<font color= "white"> Correct! </font>';
$score ++;
echo '<font color= "white">'.$score.'</font>';
} else {
echo '<font color= "white"> Wrong! </font>';
$first = $_POST['first'];
$second = $_POST['second'];
}
}
?>
</p>
</div>
<form action="<?=$_SERVER['file:///E|/php_self'];?>" method="post">
<div align="center">
<span class="style1">
<span class="style1">
<input type="hidden" name="first" value="<?=$first;?>">
<input type="hidden" name="second" value="<?=$second;?>">
<input type="hidden" name="score" value="<?=$score;?>">
<?=$first;?>
take
<?=$second;?>
equa</span>ls</span>
<input type="text" name="answer" size="3">
<input type="submit" name="mSubmit" value="Enter">
</div>
</form>
</body>
</html>


</body>
</html>

I bet its another simple mistake but any guidance would be much appreciated thanks for th help so far.

Matthew

Badman3k
11-08-2004, 01:48 PM
Okay I think your problem lies with lines:
if(!isset($_POST['score'])){ $score = 0; }
else{}
Simply remove the else{} statement and it should work.

MattK12
11-08-2004, 03:24 PM
unfortunately i added that there after i couldn't get it to work the firs time and so now i am definite that it is foxserv or something similar thanks for all the help badman, i just hope i can get it figured out.

Thanks Matthew

Badman3k
11-08-2004, 04:15 PM
I think i've picked up what the problem was, am I'm sorry that I didn't pick up on it earlier.

Your form action is pointing to file on the E drive. You need to change the form action to:

<?= $PHP_SELF;?>

i.e. your source should be:


<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
.style1 {
font-size: 16px;
font-family: Verdana, Arial, Helvetica, sans-serif;
color: #FFFFFF;
text-decoration: blink;
}
body {
background-image: url(h:My Picturesgrass.jpg);
}
.style2 {
color: #FFFFFF;
font-size: 16px;
font-family: Arial, Helvetica, sans-serif;
}
.style6 {font-size: 12px}
-->
</style>
</head>

<body bgcolor="#000000"><html>
<head>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="center">&nbsp;</p>
<title>
<div align="center" class="style1">SUBTRACTION PAGE </div>
</title>
<div align="center"> </head>
<p>
<script type="text/javascript" src="file:///E|/scripts.js"></script>
<script language="JavaScript" type="text/JavaScript">
adjust_popup();
</script><body>
<?
$first = rand(11, 25);
$second = rand(1, 10);
if(!isset($_POST['score'])){ $score = 0; }

if(isset($_POST['mSubmit'])) {
if(($_POST['first'] - $_POST['second']) == $_POST['answer']) {
echo '<font color= "white"> Correct! </font>';
$score ++;
echo '<font color= "white">'.$score.'</font>';
} else {
echo '<font color= "white"> Wrong! </font>';
$first = $_POST['first'];
$second = $_POST['second'];
}
}
?>
</p>
</div>
<form action="<?=$PHP_SELF;?>" method="post">
<div align="center">
<span class="style1">
<span class="style1">
<input type="hidden" name="first" value="<?=$first;?>">
<input type="hidden" name="second" value="<?=$second;?>">
<input type="hidden" name="score" value="<?=$score;?>">
<?=$first;?>
take
<?=$second;?>
equa</span>ls</span>
<input type="text" name="answer" size="3">
<input type="submit" name="mSubmit" value="Enter">
</div>
</form>
</body>
</html>


</body>
</html>

That should solve the problem and it shouldn't be neccessary to include the else statement.

You may also want to change the src of the JScript too :)

Let us know if this solves the prob.

Sorry I didn't pick up on it sooner, my bad :o