Jump to content

Archived

This topic is now archived and is closed to further replies.

KSquared

Session Vars

Recommended Posts

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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 array
array_slice($secProd, $id, 1);

?>[/code]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.