Jump to content

PHP - Random sum maker in 10 pages


wooxie
Go to solution Solved by Barand,

Recommended Posts

Heey,

Could some one help me to make like 5 / 10 sum generator with random codes and each sum on a other page?
Maybe you got skype and you could help me out and help me? So I got teached also icon_smile.gif

I was thinking about to make a IF - statement to make a If page <= then page 10 he will make random sums and at the end show the result?

But I don't know how I need to make that. :(

got this page so far:

<?php SESSION_START();
if(!empty($_POST['submit']))

{ $pre=$_SESSION['ans''];
$answer=$_POST['ant'];
$1= rand(0,30);
$2= rand(0,30);
$answer=$1+$2;
$_SESSION['ans']=$answer;
echo "<br>$1+$2=";
}
else
{
?>
<html>
<head>
<title>Math</title>
</head>
<body>
<h2>+</h2>
<form method='post' action=''>

Your answer:<input name='ans'><br>

</select>
<br><br>
<input name='submit' type='submit' value='next'>
<input name='reset' type='reset' value='delete'>
</form>
<?php
}
?>


Thank you~

Link to comment
Share on other sites

  • Solution

I'd do it like this

<?php
session_start();
if (!isset($_SESSION['questions'])) {
    $_SESSION['questions'] = array(
        'n1' => array(),
        'n2' => array(),
        'ans' => array()
        );
    $_SESSION['count'] = 0;
    
    $numbers = range(1,30);
    // we want to make sure that questions are not repeated
    // pick 10 numbers at random
    $arr = array_rand($numbers, 10);
    foreach ($arr as $k) $_SESSION['questions']['n1'][] = $numbers[$k];
    // remove the selected numbers from the array
    $numbers = array_diff($numbers, $_SESSION['questions']['n1']);
    // pick 10 more from the remaining numbers
    $arr = array_rand($numbers, 10);
    foreach ($arr as $k) $_SESSION['questions']['n2'][] = $numbers[$k];
}
if (isset($_POST['ans'])) {
    $_SESSION['questions']['ans'][$_SESSION['count']] = $_POST['ans'];
    ++$_SESSION['count'];
}

?>
<html>
<head>
<title>Math</title>
</head>
<body>

<form method='post' action=''>

<?php
$q = $_SESSION['count']+1;
if ($q < 11) {
    echo <<<EOT
    <h2>+</h2>
    <h5>Question $q</h5>
    {$_SESSION['questions']['n1'][$_SESSION['count']]}
    +
    {$_SESSION['questions']['n2'][$_SESSION['count']]}
    <br><br>
    Your answer: <input type='text' size='3' name='ans'><br>
    <br><br>
    <input name='btnsubmit' type='submit' value='Next'>
    <input name='btnreset' type='reset' value='Delete'>
EOT;
}
else {
    echo "<h5>Results</h5>
        <table border='1' cellpadding='3' style='border-collapse:collapse'>\n
        <tr><td></td><td>Question</td><td>Answer</td><td>Your answer</td><td>Correct</td></tr>\n";
    for ($i=0; $i<10; $i++) {
        $qno = $i+1;
        $qtext = "{$_SESSION['questions']['n1'][$i]} + {$_SESSION['questions']['n2'][$i]}";
        $ans = $_SESSION['questions']['n1'][$i] + $_SESSION['questions']['n2'][$i];
        $yours = $_SESSION['questions']['ans'][$i];
        $chk = $ans==$yours ? '&check;' : '';
        echo "<tr><td>$qno</td><td>$qtext</td><td>$ans</td><td>$yours</td><td>$chk</td></tr>\n";
    }
    echo "</table>\n<br><input name='btnsubmit' type='submit' value='New test'>";
    unset($_SESSION['questions']);
}
?>

</form>
<script type='text/javascript'>
onload = function(){document.forms[0].ans.focus();}
</script>
</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.