Jump to content

Help with calculating total score of right answers.


nitrus

Recommended Posts

Hello PHPFreaks forum,

 

 I am strugging to figure out, how to create a button that stops the rand problem generation and output the users score of correct answers.  Right now it only checks if the answers are right and says correct or if its wrong it display the correct answer. 

 

I was thinking i need to turn my random varibles into $_SESSION variables, so it stores the users data and then i can echo it back to the user.  I am not sure how to this can you please help me.  I have been working on this for week now and i am stuck!

 

 

<html>
<head>
<?php
session_start()
?>
<title>Second Grade Math quiz</title>


</head>
<body id="body">
<div id="wrapper">
<div id="header">Mutiple:</div>
<div id="quizbox">
<?php
$value1 = rand (1,10);
$value2 = rand (1,20);
?>
<form action="mathq.php" method="post">

<input type="text" name="input1a" value="<?php echo $value1; ?>" class="value" />

<input type="text" value="x" class="equa" />

<input type="text" name="input2a" value="<?php echo $value2; ?>" class="value" />

<input type="text" value="=" class="equa" />

<input id="answer" type="text" name="answer" value="" class="answer" /><br /><br />




<input type="submit" value="Submit answer" class="submit">

<INPUT TYPE="RESET" VALUE="Clear all fields of this form">

</form>

<?php

if(isset($_POST['input1a']) && isset($_POST['input2a']) && isset($_POST['answer'])) {

$result = $_POST['answer'];
$input1a = $_POST['input1a'];
$input2a = $_POST['input2a'];


if (!empty($input1a) && !empty ($input2a) && !empty($result)) {
    //echo '<span class="success">Success!  ';

}
     if ($result == $input1a * $input2a)
    {
        print($input1a . ' x ' . $input2a . ' = ' . '<span class="correct">' . $result . '</span>' . '<br />Correct!');
    
    }
    else
    {
        print($input1a . ' x ' . $input2a . ' = ' . '<span class="incorrect">' . $result . '</span>' . '<br />Wrong!<br /> The correct answer was: ' . '<span class="correct">' . $input1a * $input2a . '</span>');
    }
    
}
?>


</div>
</div>
</body>
</html>


 

 


 

Link to comment
Share on other sites

No need for a session variable, just pass it back in a hidden field:

<html>
<head>

<title>Second Grade Math quiz</title>


</head>
<body id="body">
<div id="wrapper">
<div id="header">Mutiple:</div>
<div id="quizbox">
<?php
$value1 = rand (1,10);
$value2 = rand (1,20);
?>

<?php

if(isset($_POST['input1a']) && isset($_POST['input2a']) && isset($_POST['answer'])) {

$result = $_POST['answer'];
$input1a = $_POST['input1a'];
$input2a = $_POST['input2a'];
$correct = $_POST['correct'];
$wrong = $_POST['wrong'];


if (!empty($input1a) && !empty ($input2a) && !empty($result)) {
    //echo '<span class="success">Success!  ';

}
     if ($result == $input1a * $input2a)
    {
        print($input1a . ' x ' . $input2a . ' = ' . '<span class="correct">' . $result . '</span>' . '<br />Correct!');
        $correct ++;
    
    }
    else
    {
        print($input1a . ' x ' . $input2a . ' = ' . '<span class="incorrect">' . $result . '</span>' . '<br />Wrong!<br /> The correct answer was: ' . '<span class="correct">' . $input1a * $input2a . '</span>');
        $wrong ++;
    }
    
}
?>
<form action="mathq.php" method="post">
<?php if ($_POST['s']!='Submit and Score') {?>
<input type="text" name="input1a" value="<?php echo $value1; ?>" class="value" />

<input type="text" value="x" class="equa" />

<input type="text" name="input2a" value="<?php echo $value2; ?>" class="value" />

<input type="text" value="=" class="equa" />

<input id="answer" type="text" name="answer" value="" class="answer" /><br /><br />

<input type="hidden" name="correct" value="<?php echo $correct;?>" />
<input type="hidden" name="wrong" value="<?php echo $wrong;?>" />


<input type="submit" value="Submit answer" class="submit">
<input type="submit" value="Submit and Score" name="s">

<INPUT TYPE="RESET" VALUE="Clear all fields of this form">
<?php } else {echo "Some html here so it looks good <br /> You got $correct correct and $wrong wrong";}?>

</form>


</div>
</div>
</body>
</html>
Link to comment
Share on other sites

Thanks for helping me out i can't really see the results, since im recieving : line 58 Undefined variable: correct & line 59 Undefined variable  : wrong when the submit correct button is pressed.  I also recieve a undefiend index variable on line 47 on the main page. 

 

I have had these errors alot and the only way i have fixed them as set the variable ass isset($_POST['correct'] for example.  I'm not sure if that is the correct way to fix that.

 

Please help.  Also i was trying to get the Submit correct button to output the total answers correct and the total answers wrong.

 

It might be XAMPP im running on my machine it always give me undefined index variable errors.  Would that be a setting the error reporting that needs to be changed in the php.ini file?

Edited by nitrus
Link to comment
Share on other sites

Passing the values in a hidden field is a terrible idea, unless you don't really care about your users being able to see and/or change the values as they like. Seeing as they're supposed to guess the values, I'd say it would defeat the whole process.

 

What you need to do, is to only generate new random values if none have been defined and/or if nothing have been posted. Right now you're telling the code to unconditionally create new values on every pageload.

Link to comment
Share on other sites

 

It works for me using Zend CE on a mac. Try adding lines to initialize $correct and $wrong BEFORE you set them from $_POST

 

$correct=0;
$wrong=0;
 

Its werid i get those errors only on Xampp, so i decided to port my projects to free web hoster where i can store all my projects and display the results.  It seem to show no errors vs using xampp it does.  I think xampp might have some minor problems and might not have the correct php settings.  Thanks for helping me you have been very helpful. I will try to the suggestion you stated! I'll let you know how it goes!

 

Not sure what you mean Christian F. ?

Edited by nitrus
Link to comment
Share on other sites

Which part are you not sure about?

If it's the second then I seem to have misunderstood a bit what you were trying to do, which I apologize for. Had a closer look at your second post, and I see now that you're not trying to save the actual answers in the $_SESSION array.

 

Anyway, first and foremost I'd like to have you look at the following FAQ. Preferably follow the link to the resource thread as well, and read them both carefully. This will help you with a fundamental flaw in your code structure, which prevents you from using sessions (and other headers) properly.

 

Once you've fixed that, it's as easy as using the $_SESSION array as any other array. Only with values "magically" saved across page views.

Link to comment
Share on other sites

Christian,

 

I think I disagree with your first comment.

 

Passing the values in a hidden field is a terrible idea

 

We're talking about passing the total number of correct and incorrect answers on a quiz for second graders who are wroking on problems like 6 X 7 = 42.

 

users being able to see and/or change the values as they like

 

Seeing the number they've got right before the final score is not a revelation given that the program reports each problem as correct or incorrect as they submit it. Presumably they can count so they already know how many they have right.

 

As far as changing their score, if any of them find the hidden fields and cheat then they really need to be working on more difficult material. I asked my seven year old who has mastered squares, cubes, nth roots, and imaginary numbers and who has spent a lot of time learning php with me to try cheating and he couldn't figure out how to crack it.

Link to comment
Share on other sites

Don't underestimate the ingenuity of parents ... ;)

 

At any rate, if you consider your application and target audience and decide you want to do something non-best-practice, that is certainly your call.

 

However, it is important to remember that once you learn one way of doing something (passing scores in "hidden" fields), you may get in the habit of using this practice, and end up using it in areas where it could be an issue (if you ever develop a banking system let me know, I need to add some money to my account -- through a hidden field, of course).

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.