Page 1 of 1

Multidimensional Arrays

Posted: Tue Jun 12, 2007 12:24 pm
by stakes
Hello

I'm a newbie at PHP and I've just run into Multidimensional arrays. I'm trying to make a "temporary shopping cart" with the help of sessions. Here's what i got so far.

Code: Select all

session_start();

//This function is called by an external form.

function addToCart($productID, $quantity) {


	//if the array already exists, add up the excisting "quantity" with the new quantity

	if(isset($_SESSION['product'][$productID])) {
		
      $_SESSION['product'][$productID][$quantity] = $quantity + 
      $_SESSION['product'][$productID][$quantity;
	}
	
	else {
	
	$_SESSION['product'][$productID][$quantity] = $quantity;
	
	}
	
}

function viewCart() {
	
	foreach ($_SESSION['product'] as $productID => quantity) {


              print "ID: $productID Quantity:";
	      print $_SESSION['product'][$productID][quantity];
	      print "<br />\n";

	}

}
I have a form that calls the function upon submit, where i can add a quantity. It also sends the product ID to separate products in the product array. I want this number to add up as a total sum of the $_SESSION['product'][productID] array.

However this is what happens

I add the number 1... and it adds on like i want to: 1 2 3 and so on.

I add the number 2, it appears as if it starts all over: 2, 4, 6.

But then if i add another 1, it goes back to the first and continues with 4, 5, 6.

What i want however is that the numbers add up together, within each of the different ProducID arrays.
I hope that was a somewhat accurate description of the problem. As stated earlier, I'm a real newbie at this so step by
step explanations would be very much appreciated.

Thanks in advance

/Daniel

Posted: Tue Jun 12, 2007 12:43 pm
by superdezign
You are making the quantity it's own index. So, calling addToCart would add to the index that corresponds to the quantity. So, if you were to call these:

Code: Select all

addToCart('id', 1);
addToCart('id', 1);
addToCart('id', 1);
addToCart('id', 2);
addToCart('id', 2);
You'd get these:

$_SESSION['product']['id'][1] = 3;
$_SESSION['product']['id'][2] = 4;


What you want to do is not try to give the quantities an index. Try this:

Code: Select all

function addToCart($productID, $quantity) { 


        //if the array already exists, add up the excisting "quantity" with the new quantity 

        if(isset($_SESSION['product'][$productID])) { 
               
      $_SESSION['product'][$productID]['quantity'] += $quantity; 
        } 
        
        else { 
        
        $_SESSION['product'][$productID]['quantity'] = $quantity; 
        
        } 
        
}

Posted: Tue Jun 12, 2007 5:39 pm
by aaronhall
If your products are already being stored in the database, you'll have an easier time displaying the user's shopping cart if you store the cart information in the database as well.

Posted: Wed Jun 13, 2007 6:57 am
by stakes
superdezign:

I modified the script and it is now working, thank you for explaining what i was doing wrong. Thank you very much.

aaronhall:

I solved that already i have an object that displays all the products from the SQL and i got the viewCart function to re-use the
SQL function to SELECT * where product ID = the session product id. But thank you for your suggestion.

Posted: Wed Jun 13, 2007 3:22 pm
by stakes
I crashed again. Here's my new deadly encounter :oops:

Code: Select all

function removeFromCart($id) {
	
		unset($_SESSION['product'][$id]);
}

related to the code in previous posts. Why wont the bloody session-array die? Notice: I only want to delete they key ID from the array, not the entire session product array.

Thanks in advance.

Posted: Wed Jun 13, 2007 3:31 pm
by RobertGonzalez
Set it equal to null.

Code: Select all

<?php
$var = null;
?>

Posted: Wed Jun 13, 2007 3:38 pm
by stakes
This worked, partly, however it only removed the quantity of the product somehow, and not the actual product, so it is still left in the SESSION and therefor still displays in the shopping cart, any clue why?

the whole dimesion of the array as stated previously is:

Code: Select all

$_SESSION['product'][$productID]['quantity'] = $quantity;

Posted: Wed Jun 13, 2007 4:49 pm
by RobertGonzalez
Just for the hell of it, can you do this to your original function and run it again?

Code: Select all

<?php
function removeFromCart($id) {
    if (isset($_SESSION['product'][$id])) {
        echo 'About to kill the ' . $id . ' member of the product session array';
        unset($_SESSION['product'][$id]);
        echo '<br />OK, Kilt it';
        return;
    }

    echo 'There was not session array member with the id of '. $id . '...';
}
?>

Posted: Wed Jun 13, 2007 5:10 pm
by stakes
I get your error message.
There was not session array member with the id of 1...
The array was set using the following:

Code: Select all

$_SESSION['product'][$ID] = $quantity;
where $id is 1, and $quantity is 1

Posted: Wed Jun 13, 2007 5:29 pm
by RobertGonzalez
The reason the session array value is not dying like you want it to is that it doesn't exist the way you think it does.

I would suggest a var_dump($_SESSION) to see what is in the session array.

Posted: Wed Jun 13, 2007 6:02 pm
by stakes
Solved it, I'm sorry it all came down to a little stupid typo, sigh it's getting late i better go sleep.

Thanks a lot for your help anyways, very much appreciated.

Posted: Wed Jun 13, 2007 6:37 pm
by RobertGonzalez
Just tell us, did it have anything to do with the uppercase $ID?

Posted: Sun Jun 17, 2007 9:52 am
by stakes
No, i just misspelled "prodcut".