Jump to content

selecting with rand() from an array


experience

Recommended Posts

i select a random number from an array.

[code]$base = array("1","2","3","4","5","6","7","8");
$var = $base[rand(0,7)];
echo "<br>\$var = $var";
[/code]
think there is a sumbit button bottom of this code.
when i press submit. i want $var to be static and show the number (which is selected once from array with rand() )
after i press the submit button again i want the first number should stay in the screen and $var should choose a new value from array randomly.

for example.

i pressed submit once.
output maybe:

[code]$var=3[/code]

then i pressed submit again:
output: ($var should still be 3)

[code]$var=3
$var=7[/code]


then i pressed submit again;
output:
[code]
$var=3
$var=7
$var=2[/code]

like these.
how can i do that ?
Use a session variable to store the selections between submissions

[code]<?php
session_start();

$vars = isset($_SESSION['vars']) ? $_SESSION['vars'] : array();  // if you have values, get them

$base = range(1,8);

$vars[] = $base[rand(0,7)];

$_SESSION['vars'] = $vars;  // store selected values

foreach ($vars as $var) echo "<br>$var";

?>
<form>
<INPUT TYPE='SUBMIT'  name='submit' value='Submit'>
</form>[/code]
Well, I was writing essentially the same thing when Barand posted, but figured I'd post what I had as well. The main difference is that I included a "clear" button to clear out the session, but that's about it...

[code]<?php
session_start();
?>
<html>
<head></head>
<body>
<?php
if (isset($_POST['submit'])) {
    $_SESSION['base'] = array("1","2","3","4","5","6","7","8");
    $_SESSION['var'][] = $_SESSION['base'][rand(0,7)];
    foreach ($_SESSION['var'] as $value) {
        echo "\$var = $value<br />";
    }
}
if (isset($_POST['clear'])) {
    session_destroy();
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input name="submit" type="submit" value="Submit">
<input name="clear" type="submit" value="Clear">
</form>
</body>
</html>[/code]
Just to put it out there, instead of using a session I have a way that might work just as well.

[code]
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<?php
  $var = rand(1,8);
  if ( is_array($_POST) ) {
    while (list($key, $val)=each($_POST)) {
      echo "<input type=\"hidden\" id=\"val".$key."\" name=\"val".$key."\" value=\"".$val."\" />\r\n";
    }
    echo "<input type=\"hidden\" id=\"val".count($_POST)."\" name=\"val".count($_POST)."\" value=\"".$var."\" />\r\n";
  } else {
    echo "<input type=\"hidden\" id=\"val0\" name=\"val0\" value=\"".$var."\" />\r\n";
  }
?>
  <input type="submit" value="Get Another One" />
</form>
[/code]

This will retain old values and create new ones without using sessions. It is just about as simple as I can think to make it and still retain some error control. Just another way to reach the same end.

Happy coding!

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.