Jump to content

Session Issues.


vmicchia

Recommended Posts

I have a file that creates sessions and adds to them for a shopping cart. On one site it works fine. On a second it seems to have issues when adding 2 of the same category of product. I have attached the file. when I echo the sessions with one product I get this:

Array
(
    [total] => 1
    [cushIdentifier] => 1
    [cushArray] => Array
        (
            [0] => Array
                (
                    [Cushion] => Boxed Chair Cushion
                    [Fabric] => F-001H
                    [FabricPrice] => 0
                    [Fill] => Layered Polyfiber
                    [button] => 2
                    [ContWelt] => none
                    [ContWeltFabric] => none
                    [Zipper] => N
                    [Quantity] => 1
                    [WeltSize] => 
                    [sKU] => JBBC2244
                    [Edge] => Boxed Edge
                    [Cap] => N
                    [Hector] => N
                    [stitch] => 
                    [TopStitch] => N
                    [Ties] => Y
                    [Measure] => 136
                    [straps] => N
                    [Hinged] => Y
                    [Type] => 
                    [Price] => 111.39
                    [Total] => 111.39
                    [backHeight] => N/A
                    [backWidth] => N/A
                    [backThick] => N/A
                    [seatDepth] => N/A
                    [seatWidth] => N/A
                    [seatThick] => N/A
                    [WeltTape] => N/A
                )

        )

)

 

But when I add a second it does this:

(
    [total] => 6
    [cushIdentifier] => 2
    [cushArray] => Array
        (
            [0] => Boxed Chair Cushion
            [1] => F-009H
            [2] => 0
            [3] => Layered Polyfiber
            [4] => 2
            [5] => none
            [6] => none
            [7] => N
            [8] => 1
            [9] => 
            [10] => JBBC2244
            [11] => Boxed Edge
            [12] => N
            [13] => N
            [14] => 
            [15] => N
            [16] => Y
            [17] => 136
            [18] => N
            [19] => Y
            [20] => 
            [21] => 111.39
            [22] => 111.39
            [23] => N/A
            [24] => N/A
            [25] => N/A
            [26] => N/A
            [27] => N/A
            [28] => N/A
            [29] => N/A
            [30] => Array
                (
                    [Cushion] => Boxed Chair Cushion
                    [Fabric] => F-009H
                    [FabricPrice] => 0
                    [Fill] => Layered Polyfiber
                    [button] => 2
                    [ContWelt] => none
                    [ContWeltFabric] => none
                    [Zipper] => N
                    [Quantity] => 1
                    [WeltSize] => 
                    [sKU] => JBBC2244
                    [Edge] => Boxed Edge
                    [Cap] => N
                    [Hector] => N
                    [stitch] => 
                    [TopStitch] => N
                    [Ties] => Y
                    [Measure] => 136
                    [straps] => N
                    [Hinged] => Y
                    [Type] => 
                    [Price] => 111.39
                    [Total] => 111.39
                    [backHeight] => N/A
                    [backWidth] => N/A
                    [backThick] => N/A
                    [seatDepth] => N/A
                    [seatWidth] => N/A
                    [seatThick] => N/A
                    [WeltTape] => N/A
                    [1] => Array
                        (
                            [Cushion] => Boxed Chair Cushion
                            [Fabric] => F-009H
                            [FabricPrice] => 0
                            [Fill] => Layered Polyfiber
                            [button] => 2
                            [ContWelt] => none
                            [ContWeltFabric] => none
                            [Zipper] => N
                            [Quantity] => 1
                            [WeltSize] => 
                            [sKU] => JBBC2244
                            [Edge] => Boxed Edge
                            [Cap] => N
                            [Hector] => N
                            [stitch] => 
                            [TopStitch] => N
                            [Ties] => Y
                            [Measure] => 136
                            [straps] => N
                            [Hinged] => Y
                            [Type] => 
                            [Price] => 111.39
                            [Total] => 111.39
                            [backHeight] => N/A
                            [backWidth] => N/A
                            [backThick] => N/A
                            [seatDepth] => N/A
                            [seatWidth] => N/A
                            [seatThick] => N/A
                            [WeltTape] => N/A
                            [1] => 
                        )

                )

        )

)

 

[attachment deleted by admin]

Link to comment
Share on other sites

I haven't looked through all your code - especially since it is >600 lines any you didn't bother to at least identify the lines that would have some effect on the problem. But, here is at least one problem:

 

if(isset($_SESSION['cushArray'])){
$_SESSION['cushArray'] = array_values($_SESSION['cushArray']);
}

 

What exactly are you trying to do here? That code is basically saying if the session value exists reset it's value with the same values???

 

Anyway, if you were to check the manual for array_values() you would see this:

array_values() returns all the values from the input array and indexes numerically the array.

 

So, you are replacing the array with named indexes with numerically based indexes. That explains the values being associated with numerically based indexes on the first level. Not sure why you have the same values duplicated in two sub array, but I'm guessing both of those are from the same defect.

 

I tried to see if there were any other apparent problems, but I see no flow/structure to your code. And - ARE YOU KIDDING ME - not one single comment?! Comments should be considered a neccessity of coding.

Link to comment
Share on other sites

Ok I apologize. I have taken out the array values but that didn't help. Here is a cut down version of the code for just one product, because the rest is just repeating for different products and has the same issue:

if($_POST['product'] == 'cushion'){
// Set up all the needed variables
if(empty($_POST['sku'])){
$_SESSION['orderError'] = 'There was an error please try again';
header($prevpage);
}else{
$sku = $_POST['sku'];
}

if (!isset($_SESSION['cushIdentifier'])){
   			$_SESSION['cushIdentifier'] = 1;
}
   
if(!isset($_SESSION['cushArray'])){
	$_SESSION['cushArray'] = array();
}
if(empty($_POST['cushionName'])){	
$_SESSION['orderError'] = 'No cushion selected.';
header($prevpage);
}else{
$cushion = $_POST['cushionName'];
}
if(empty($_POST['fabricSelect'])){
$fabric = 'none';
}else{
$fabric = $_POST['fabricSelect'];
}
if(empty($_POST['fill'])){
$_SESSION['orderError'] = 'No Fill Selected';
header($prevpage);
}else{
$fill = $_POST['fill'];
}
if(empty($_POST['buttonSelect'])){
$button = 'none';
}else{
$button = $_POST['buttonSelect'];
}
if(empty($_POST['contWelt'])){
$contWelt = 'none';
}else{
$contWelt = $_POST['contwelt'];
}
if(empty($_POST['weltFabricSelect'])){
$contWeltFab = 'none';
}else{
$contWeltFab = $_POST['weltFabricSelect'];
}
if(empty($_POST['zipper'])){
$zip = 'none';
}else{
$zip = $_POST['zipper'];
}
//echo $_POST['quant'];
if(empty($_POST['quant']) || $_POST['quant'] == '0'){
$_SESSION['orderError'] = 'Please select the number of cushions you would like to order.';
header('Location:'.$prevpage);
}else{
$quantity = $_POST['quant'];
}
if(empty($_POST['weltSize'])){
$weltSize = 'standard';
}else{
$weltSize = $_POST['WeltSize'];
}
if(empty($_POST['edge'])){
$edge = 'standard';
}else{
$edge = $_POST['edge'];
}
if(empty($_POST['cap'])){
$cap = 'none';
}else{
$cap = $_POST['cap'];
}
$hinged = $_POST['hinged'];
$straps = $_POST['straps'];

$type = $_POST['type'];
$ties = $_POST['ties'];
if(empty($_POST['BackHeight'])){
	$bh = 'N/A';
}else{
	$bh = $_POST['BackHeight'];
}

if(empty($_POST['BackWidth'])){
	$bw = 'N/A';
}else{
	$bw = $_POST['BackWidth'];
}

if(empty($_POST['BackThick'])){
	$bt = 'N/A';
}else{
	$bt = $_POST['BackThick'];
}

if(empty($_POST['SeatDepth'])){
	$sd = 'N/A';
}else{
	$sd = $_POST['SeatDepth'];
}

if(empty($_POST['SeatWidth'])){
	$sw = 'N/A';
}else{
	$sw = $_POST['SeatWidth'];
}

if(empty($_POST['SeatThick'])){
	$st = 'N/A';
}else{
	$st = $_POST['SeatThick'];
}

if(empty($_POST['weltTapeSelect'])){
	$wt = 'N/A';
}else{
	$wt = $_POST['weltTapeSelect'];
}


if(empty($_POST['price']) || $_POST['price'] == '0.00'){
$_SESSION['orderError'] = 'There was an error please try again';
header($prevpage);
}else{
$price = $_POST['price'];
}
//echo $quantity.'<br />';
//echo $price.'<br />';
//echo $fabricPrice.'<br />';
//echo ($quantity * $fabricPrice).'<br />';
$total = ($quantity * $price) + ($quantity * $fabricPrice);
//echo $total;
////////////////////////////////////////////////////////
///////SET THE ARRAY///////////////////////////////////
$cushArray = array(
				Cushion=> $cushion,
				Fabric=> $fabric,
				FabricPrice=> $fabricPrice,
				Fill=> $fill,
				Button=> $button,
				ContWelt=> $contWelt,
				ContWeltFabric=> $contWeltFab,
				Zipper=> $zip,
				Quantity=> $quantity,
				WeltSize=> $weltsize,
				SKU=> $sku,
				Edge=> $edge,
				Cap=> $cap,
				Hector=> $_POST['hector'],
				Stitch=> $_POST['stitch'],
				TopStitch=> $_POST['topstitch'],
				Ties=> $_POST['ties'],
				Measure=> $_POST['measurements'],
				Straps=> $straps,
				Hinged=> $hinged,
				Type=> $type,
				Price=> $price,
				Total=> $total,
				BackHeight=> $bh,
				BackWidth=> $bw,
				BackThick=> $bt,
				SeatDepth=> $sd,
				SeatWidth=> $sw,
				SeatThick=> $st,
				WeltTape=> $wt,
				);
//give the array an number and save to the session.					
$_SESSION['cushArray'][$_SESSION['cushIdentifier']] = $cushArray;
	$_SESSION['cushIdentifier']++;
}

 

Once again the problem is when I get 2 products into the array it gets messed up.(same as in above post)

 

Link to comment
Share on other sites

Also here's what the array output is supposed to look like with 2 products.(this is what the site that works, and has the same code, shows.):

Array

(

    [0] => Array

        (

            [Cushion] => Boxed Chaise Lounge Cushion

            [Fabric] => F-005E

            [FabricPrice] => 0

            [Fill] => Layered Polyfiber

            [button] => 3

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 1

            [WeltSize] =>

            [sKU] => JBBL2373

            [Edge] => Boxed Edge

            [Cap] => N

            [Hector] => N

            [stitch] =>

            [TopStitch] => N

            [Ties] => Y

            [Measure] => 141

            [straps] => N

            [Hinged] => Y

            [Type] =>

            [Price] => 150.33

            [Total] => 150.33

            [backHeight] => N/A

            [backWidth] => N/A

            [backThick] => N/A

            [seatDepth] => N/A

            [seatWidth] => N/A

            [seatThick] => N/A

            [WeltTape] => N/A

        )

 

    [1] => Array

        (

            [Cushion] => French Seam Chair Cushion

            [Fabric] => F-776D

            [FabricPrice] => 0

            [Fill] => Layered Polyfiber

            [button] => 1

            [ContWelt] => none

            [ContWeltFabric] => none

            [Zipper] => N

            [Quantity] => 1

            [WeltSize] =>

            [sKU] => JFSBC2036

            [Edge] => Knife Edge

            [Cap] => N

            [Hector] => N

            [stitch] =>

            [TopStitch] => N

            [Ties] => Y

            [Measure] => 147

            [straps] => N

            [Hinged] => Y

            [Type] =>

            [Price] => 58.71

            [Total] => 58.71

            [backHeight] => N/A

            [backWidth] => N/A

            [backThick] => N/A

            [seatDepth] => N/A

            [seatWidth] => N/A

            [seatThick] => N/A

            [WeltTape] => N/A

        )

 

)

Link to comment
Share on other sites

Sorry, but your code is just way too overcomplicated and too long for me to try and analyze. You need to add some debugging code to validate the values that you expect at each step of the process. For example, if you modify an array, do a print_r() before and after you modify it to ensure it is being modified as you expect.

 

But, as I alluded to previously, you need to know what you are doing and, more importantly, why. By including comments you can provide clarification as the logic flow in your code. Regarding the line you took out, what was the purpose of the line? You obviously thought you needed it for some reason. Then you take it completely out because of my comments.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.