Jump to content


Photo

Session Vars


  • Please log in to reply
4 replies to this topic

#1 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 October 2006 - 05:33 PM

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?

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);
	}
}

Thanks

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 20 October 2006 - 06:01 PM

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 not 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 reference to your session variable instead of copying it:
<?php
$secProd =& $_SESSION['secProd'];
?>

by doing it this way, whatever you act on $secProd will be reflected in $_SESSION['secProd']

hope this helps!
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 October 2006 - 06:21 PM

Thanks for the help!  Its working now.

#4 KSquared

KSquared
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 20 October 2006 - 07:54 PM

Actually....

One other thing... Shouldnt this then remove the requested value from the array?
It seems to do nothing.


<?php

$secProd =& $_SESSION['secProd'];
$id = $_GET['id']; //the index of the array of which product to remove from array
array_slice($secProd, $id, 1);

?>


#5 Psycho

Psycho
  • Moderators
  • Move along, nothing to see here
  • 11,892 posts
  • LocationCanada

Posted 20 October 2006 - 08:41 PM

No, array_slice returns 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);
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users