Jump to content

PHP - Random sum maker in 10 pages


wooxie

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
https://forums.phpfreaks.com/topic/284729-php-random-sum-maker-in-10-pages/
Share on other sites

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.