chaseman Posted March 1, 2011 Share Posted March 1, 2011 Is it possible to remember the last created random number with PHP or do I need to make use of MySQL? I want to generate an equation with submit button 1 and echo it out, and then I want the script to calculate the printed out equation with submit button 2. The problem: As soon as I click submit button 2, the numbers change into different random numbers, since the PHP starts scanning from the top of the file again as soon as the second button has been pressed. $gen = $_POST['gen']; $calc = $_POST['calc']; $x1 = random_number (); $x2 = random_number (); $op = random_op (); echo " <form action='' method='POST'> <input type='submit' name='gen' value='Generate' /><br /> <input type='submit' name='calc' value='Calculate' /> </form> "; if ($gen) { echo "$x1 $op $x2 = ? <br /><br />"; } if ($calc) { switch ($op) { case '+' : $row_1 = $x1 + $x2; break; case '-' : $row_1 = $x1 - $x2; break; case '*' : $row_1 = $x1 * $x2; break; case '/' : $row_1 = $x1 / $x2; break; } echo "$x1 $op $x2 = $row_1"; } I could of course insert the generated numbers into the database and fetch it out again, but I was wondering if it's possible to remember the numbers (even when the 2nd submit buttons has been pressed) within PHP as well? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/229325-possible-to-remember-the-last-created-random-number-with-php/ Share on other sites More sharing options...
Zane Posted March 1, 2011 Share Posted March 1, 2011 Try using sessions Quote Link to comment https://forums.phpfreaks.com/topic/229325-possible-to-remember-the-last-created-random-number-with-php/#findComment-1181606 Share on other sites More sharing options...
chaseman Posted March 1, 2011 Author Share Posted March 1, 2011 Didn't work either, the random numbers simply will be re-inserted into the sessions. $gen = $_POST['gen']; $calc = $_POST['calc']; $_SESSION['x1'] = random_number (); $_SESSION['x2'] = random_number (); $_SESSION['op'] = random_op (); $x1 = $_SESSION['x1']; $x2 = $_SESSION['x2']; $op = $_SESSION['op']; echo " <form action='' method='POST'> <input type='submit' name='gen' value='Generate' /><br /> <input type='submit' name='calc' value='Calculate' /> </form> "; if ($gen) { echo "$x1 $op $x2 = ? <br /><br />"; } if ($calc) { switch ($op) { case '+' : $row_1 = $x1 + $x2; break; case '-' : $row_1 = $x1 - $x2; break; case '*' : $row_1 = $x1 * $x2; break; case '/' : $row_1 = $x1 / $x2; break; } echo "$x1 $op $x2 = $row_1"; } Quote Link to comment https://forums.phpfreaks.com/topic/229325-possible-to-remember-the-last-created-random-number-with-php/#findComment-1181614 Share on other sites More sharing options...
Zane Posted March 1, 2011 Share Posted March 1, 2011 Perhaps a ternary statement as well then. $_SESSION['x1'] = isset($_SESSION['x1']) ? $_SESSION['x1'] : random_number (); $_SESSION['x2'] = isset($_SESSION['x2']) ? $_SESSION['x2'] : random_number (); $_SESSION['op'] = isset($_SESSION['x3']) ? $_SESSION['x3'] : random_op (); Quote Link to comment https://forums.phpfreaks.com/topic/229325-possible-to-remember-the-last-created-random-number-with-php/#findComment-1181615 Share on other sites More sharing options...
chaseman Posted March 2, 2011 Author Share Posted March 2, 2011 Where do you even learn this? I can't find any books teaching this type of stuff. I learn from this forum more than out of books, without this forum I'd have never learned PHP lol Here's the equation script, when I press generate, it will generate an equation, and when I press calc it will calculate it, if I press generate again, it will generate a new equation, SINCE the last session variable has been unset at the bottom of the page. Pretty cool stuff, I love the feeling of accomplishment lol $gen = $_POST['gen']; $calc = $_POST['calc']; $_SESSION['x1'] = isset($_SESSION['x1']) ? $_SESSION['x1'] : random_number (); $_SESSION['x2'] = isset($_SESSION['x2']) ? $_SESSION['x2'] : random_number (); $_SESSION['op'] = isset($_SESSION['op']) ? $_SESSION['op'] : random_op (); $x1 = $_SESSION['x1']; $x2 = $_SESSION['x2']; $op = $_SESSION['op']; echo " <form action='' method='POST'> <input type='submit' name='gen' value='Generate' /><br /> <input type='submit' name='calc' value='Calculate' /> </form> "; if ($gen) { echo "$x1 $op $x2 = ? <br /><br />"; return; } if ($calc) { switch ($op) { case '+' : $row_1 = $x1 + $x2; break; case '-' : $row_1 = $x1 - $x2; break; case '*' : $row_1 = $x1 * $x2; break; case '/' : $row_1 = $x1 / $x2; break; } echo "$x1 $op $x2 = $row_1"; } unset ($_SESSION['x1']); unset ($_SESSION['x2']); unset ($_SESSION['op']); Thanks Zanus! Quote Link to comment https://forums.phpfreaks.com/topic/229325-possible-to-remember-the-last-created-random-number-with-php/#findComment-1181620 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.