Jump to content


Photo

selecting with rand() from an array


  • Please log in to reply
4 replies to this topic

#1 experience

experience
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 March 2006 - 04:19 AM

i select a random number from an array.

$base = array("1","2","3","4","5","6","7","8");
$var = $base[rand(0,7)];
echo "<br>\$var = $var";
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:

$var=3

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

$var=3
$var=7


then i pressed submit again;
output:
$var=3
$var=7
$var=2

like these.
how can i do that ?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 07 March 2006 - 07:13 PM

Use a session variable to store the selections between submissions

<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 kanikilu

kanikilu
  • Members
  • PipPip
  • Member
  • 29 posts
  • LocationDallas, TX

Posted 07 March 2006 - 07:32 PM

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

<?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>


#4 experience

experience
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 07 March 2006 - 11:18 PM

thank you a lot :)
i love phpfreaks

#5 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 08 March 2006 - 07:38 AM

Just to put it out there, instead of using a session I have a way that might work just as well.

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

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!

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users