Jump to content

Recommended Posts

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.

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";

} 

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 ();

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!

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.