Multidimensional Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Multidimensional Arrays

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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; 
        
        } 
        
}
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post 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.
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post 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.
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Set it equal to null.

Code: Select all

<?php
$var = null;
?>
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post 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;
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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 . '...';
}
?>
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Just tell us, did it have anything to do with the uppercase $ID?
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Post by stakes »

No, i just misspelled "prodcut".
Post Reply