screenshotcentral Posted March 30, 2006 Share Posted March 30, 2006 I have a slight issue regarding session variables and arrays.[code]<?session_start();$ID=$_POST['ID'];session_register('gun_array');$_SESSION['gun_array']=$array;$array = array();$array[]=$ID;[/code]This is what I have coded. I want start out with a blank array, then every time you access a certain page, a new $ID should be entered into the array. This value all depends on selections made on the previous pages. The problem I am having is that the new id just replaces the old id, and does not make an array. When I echo the array, its only give the one value, and is replaced when I change $ID. I read before that $array[]=(variable) is equivalent to array_push, but if it is, what else am I doing wrong, since array_push does not work either? Quote Link to comment Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 Never ever ever use session_register...it's an old function! ESPECIALLY if you have register globals on!Anways, here's your script:[code]<?phpsession_start();$id = $_POST['ID'];if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array();$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );?>[/code] Quote Link to comment Share on other sites More sharing options...
screenshotcentral Posted March 30, 2006 Author Share Posted March 30, 2006 When I used that script, the php gave an error saying [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: array_push(): First argument should be an array (wesite path removed) on line 8[/quote]7 if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array();8 $_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );I understand that in order to push an array, an array must be stated inside of array_push(array, variable). When the script ran, I am guessing that it decided that $_SESSION_['gun_array'] was not a valid array to push. Also the code that previously worked to view the data was [code]<? session_start();foreach($_SESSION['gun_array'] as $value) {echo("$value<BR>");}?>[/code] but that does not seem to work now, which I assume is because the session variable fails to register as a legitmate array. Quote Link to comment Share on other sites More sharing options...
Guest footballkid4 Posted March 30, 2006 Share Posted March 30, 2006 I apologize, that was my fault...try this:[code]<?phpsession_start();$id = $_POST['ID'];if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array( rand() );$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $id );?>[/code]As you can see, if session gun_array isn't set, we start it with rand() as one of the array values. So, when you loop through them...just exclude the first entry. Quote Link to comment Share on other sites More sharing options...
screenshotcentral Posted March 31, 2006 Author Share Posted March 31, 2006 Sadly, that does not work either, it gives the exact same error on line 8, but I thank you for helping. I guess what I need to do is to learn more about sessions mixed with arrays in order to help work out these problems in the future. Do you know of anywhere where I can get information on session variables used as arrays? I have searched the net many times before coming here, but I could not find much of anything significant. The manual for php either has topics on sessions or ones on arrays, but never a mixture of the two.Thanks again.Exact code on the page:[code]<?session_start();$ID=$_POST['ID'];$NAME=$_POST['NAME'];$PRICE=$_POST['PRICE'];if ( ! isset( $_SESSION['gun_array'] ) ) $_SESSION['gun_array'] = array( rand() );$_SESSION['gun_array'] = array_push( $_SESSION['gun_array'] , $ID );?>[/code] 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.