fortnox007 Posted September 17, 2010 Share Posted September 17, 2010 Hi all a small question. I was wondering if someone knows what would be the approach to achieve the following. on a page I show a random number. If the page refreshes (selfreferencing) It shows a new random number but also a the previous number, and this must be able to repeat itself : ) So to make it more visible: New Random number = 98765 Previous Number = 56412 This is what I have but it's ofc not working because ones the session var has been set it will not show the previous variable but the very first one. <?php session_start(); $random_number .= mt_rand(1,100000); if(!isset($_SESSION['number'])){ $_SESSION['number']=$random_number; } // echo all out: echo 'New random number is: '.$random_number; echo 'Previous number is: '.$_SESSION['number']; ?> I bet i need another variable to store the previous one in but for some reasons my brains don't work at the moment Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/ Share on other sites More sharing options...
Pikachu2000 Posted September 17, 2010 Share Posted September 17, 2010 So each time the page is refreshed, you want to display the current random number, and all previously generated numbers, or just the current one and last one? Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111905 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 Only the current one and the previous, but if you know a way to view last few that might give more insight in it. I am allready trying stuff local with unset() but no succes yet I can do it with hiddenfields but i rather do it with sessions, and it's also good for better understanding them I think Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111906 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 Ok I think I have it . The code below only shows the previous one and the current one. Pikachu2000 (or any other smart person) If you know a way how to show a list of all the previous ones I would love to here it ; ) I can think of a way but that is pretty redundant. session_start(); $_SESSION['old']=$_SESSION['number'];//placed above the random generated one $random_number .= mt_rand(1,100000); $_SESSION['number']=$random_number; // echo all out: echo 'New random number is: '.$random_number.'<br />'; echo 'Just to store the current: '.$_SESSION['number'].'<br />'; echo 'Previous number is: '.$_SESSION['old'].'<br />'; Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111910 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 Ok I adjusted alittle more and i think I am almost able to store the previous numbers in an array, but it seems i am doing something wrong, because it overrides the value in the array instead of putting it at the bottom. Btw i didn't use array_push because the manual said it's for multiple values. Could anyone see the error? $_SESSION['list_of_numbers']=array(); $_SESSION['list_of_numbers'][]=$_SESSION['old'];// why doesn't this add a value to the bottom of the array? foreach($_SESSION['list_of_numbers'] as $key=>$value) { // and print out the values echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />'; } Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111918 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 HA i found it!!! wohoOOO The script above made a new array everytime the page refreshed i think so i placed an if statement and here it is : ) if(!isset($_SESSION['list_of_numbers'])){ $_SESSION['list_of_numbers']=array();// now it will only be created if it doesn't exist } $_SESSION['list_of_numbers'][]=$_SESSION['old']; foreach($_SESSION['list_of_numbers'] as $key=>$value) { // and print out the values echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />'; } If someone has a better method please tell me : ) Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111920 Share on other sites More sharing options...
Pikachu2000 Posted September 17, 2010 Share Posted September 17, 2010 This may not be perfect, since my brain is a little fried at the moment, but you can also try this if you'd like . . . Just paste it in to a new .php file to try it out. <?php session_start(); function GENERATE_RAND($how_many) { if( count($_SESSION['rand']) > $how_many || !isset($_SESSION['rand']) ) { //if the array was already over the parameter, initialize it as empty $_SESSION['rand'] = array(0=>'', 1=>'', 2=>'', 3=>''); } else { array_unshift($_SESSION['rand'], mt_rand(1,100000)); if( count($_SESSION['rand']) >= $how_many ) { array_pop($_SESSION['rand']); } } return $_SESSION['rand']; } // USAGE // $rand_array = GENERATE_RAND(4); // 4 is the parameter passed to $how_many, determines number of rands to show in history. print_r($rand_array); ?> Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1111933 Share on other sites More sharing options...
fortnox007 Posted September 17, 2010 Author Share Posted September 17, 2010 OH nice, Thanks Pikachu2000! I never really worked with arrays until yesterday. Seems they are quite fun Ty! Link to comment https://forums.phpfreaks.com/topic/213621-small-session-question/#findComment-1112066 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.