简体   繁体   中英

calculate total score in percentage

<?php
$value1 = rand (1,10);
$value2 = rand (1,10);
?>
<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!&nbsp;&nbsp;';

}
     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>');
    }

}
?>

I was wondering how i could calculate total score of right answers after the user hits the submit button? How could i create a loop that would go through the rand number of problems and after it goes through once it exits and outputs the person score as percentage of right or wrong answers. I'm stuck on this part.

I was also thinking of another way i could tackle this problem.

A user goes through the rand number of math problems once he is done with practices problem he or she hits Done button and it outputs his score. Can you give me guidance how i can do this? Thanks.

You need a place to store data whilst questions are in progress.

This would commonly be done in Session data, or in a local database. It could be done in cookies, but not advisable. It could also be done in a flat text file.

You'd need to count how many questions done, how many right answers, and you can then derive the resulting % to feed back to the user.

Without somewhere to store the intermediate data, you cannot do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM