experience Posted March 7, 2006 Share Posted March 7, 2006 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 ? Quote Link to comment Share on other sites More sharing options...
Barand Posted March 7, 2006 Share Posted March 7, 2006 Use a session variable to store the selections between submissions[code]<?phpsession_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 valuesforeach ($vars as $var) echo "<br>$var";?><form><INPUT TYPE='SUBMIT' name='submit' value='Submit'></form>[/code] Quote Link to comment Share on other sites More sharing options...
kanikilu Posted March 7, 2006 Share Posted March 7, 2006 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]<?phpsession_start();?><html><head></head><body><?phpif (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] Quote Link to comment Share on other sites More sharing options...
experience Posted March 7, 2006 Author Share Posted March 7, 2006 thank you a lot :)i love phpfreaks Quote Link to comment Share on other sites More sharing options...
txmedic03 Posted March 8, 2006 Share Posted March 8, 2006 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! Quote Link to comment 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.