KSquared Posted October 20, 2006 Share Posted October 20, 2006 Hello All,Im having an issue with session vars. The first time a user lands on this page if the SESSION var 'secProd' is not set, go ahead and set it and add the product info. That works fine.The else is for when a user adds another product. I seem to lose $secProd data type as array. It comes back true, so I know the var itself exists but no longer an array.... any suggestions?[code]session_start();$prod = $_GET['product'];$model = $_GET['model'];$action = $_GET['action'];if($action != "view"){ if(!$_SESSION['secProd']){ $secProd = $_SESSION['secProd'] = array(); $prodArray = array(); $add = "$model^$prod"; array_push($prodArray, $add); $_SESSION['secProd'] = array_push($secProd, $prodArray); }else{ $secProd = $_SESSION['secProd']; $prodArray = array(); $add = "$model^$prod"; array_push($prodArray, $add); array_push($secProd, $prodArray); }}[/code]Thanks Quote Link to comment Share on other sites More sharing options...
obsidian Posted October 20, 2006 Share Posted October 20, 2006 you're not adding to the $_SESSION variable at all. remember that when you pass the value of an array, you are passing the VALUE to the new variable. therefore, when you update that new variable, your original variable (in this case, your session variable) does [b]not[/b] get altered. you need to call your functions on the session variable directly or else apply the value of $secProd back to your session variable. one other option, and possibly your best one in this case, is to tell PHP to use $secProd as a [b]reference[/b] to your session variable instead of copying it:[code]<?php$secProd =& $_SESSION['secProd'];?>[/code]by doing it this way, whatever you act on $secProd will be reflected in $_SESSION['secProd']hope this helps! Quote Link to comment Share on other sites More sharing options...
KSquared Posted October 20, 2006 Author Share Posted October 20, 2006 Thanks for the help! Its working now. Quote Link to comment Share on other sites More sharing options...
KSquared Posted October 20, 2006 Author Share Posted October 20, 2006 Actually....One other thing... Shouldnt this then remove the requested value from the array?It seems to do nothing.[code]<?php$secProd =& $_SESSION['secProd'];$id = $_GET['id']; //the index of the array of which product to remove from arrayarray_slice($secProd, $id, 1);?>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 20, 2006 Share Posted October 20, 2006 No, array_slice [b]returns[/b] a value based upon the array used in the function. It does not change the value used within the function. In your code above that line does absolutely nothing. You need to set the value like this:$secProd = array_slice($secProd, $id, 1); 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.