Jump to content

[SOLVED] arrays for cart


M.O.S. Studios

Recommended Posts

hey guys im attempting my own cart. here is my prob, i have a script that reads $_GET dadt and places it into a array variable, how ever it only seem to work once, im havingtrouble getting the two to combine, here is my script

 

 

here is the url it see has blahblah/cart.php?action=add&cat=hotrods&item_number=B001

<?php

session_start();

$cart = $_SESSION['cart'];

 

if($_GET['action']==add)

{

if($cart)

{

$cart .=",". array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1);

}

else

{

  $cart=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1);

}

$_SESSION['cart'] = $cart;

}

 

$cart=array($_SESSION['cart']);

echo "catagory item qty <br/>";

echo $cart['cat']." ".$cart['item_number']." ".$cart['qty'];

//echo "<br/>"." ".$cart['1']['cat']." ".$cart['1']['item_number']." ".$cart['1']['qty'];

print_r ($cart);

 

this first time i add that prod it give me this

 

 

Array ( [0] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1 ) )

 

what i want, is if i click the prod again it will give me this

 

Array ( [0] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1,

          [1] => Array ( [cat] => hotrods [item_number] => B001 [qty] => 1)

 

instead it gives me this:

 

Array ( [0] => Array,Array )

 

can any one see my mistake?

 

?>

 

 

 

thanks,

 

Jarred f.

Link to comment
Share on other sites

Try:

<?php

session_start();

$cart = $_SESSION['cart'];

 

if($_GET['action']==add)

{

  $cart[]=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1);

$_SESSION['cart'] = $cart;

}

 

It'll be a multi-dimensional array of products.

 

Link to comment
Share on other sites

WOW! thanks for that amazingly fast reply!!!

 

i tried your code and it gave me this error:

 

Fatal error: [] operator not supported for strings in /home/montrea1/public_html/lm/cart/cart.php on line 7

 

 

here is the new code

 

<?php

session_start();

$cart = $_SESSION['cart'];

 

if($_GET['action']==add)

{

$cart[]=array(cat=> $_GET['cat'], item_number=> $_GET['item_number'], qty=>1);

$_SESSION['cart'] = $cart;

}

 

print_r ($cart);

?>

 

any ideas?

Link to comment
Share on other sites

Also, change it to this:

 

<?php

session_start();

$cart = $_SESSION['cart'];

 

if($_GET['action']==add)

{

$cart[]=array('cat'=> $_GET['cat'], 'item_number' => $_GET['item_number'], 'qty' =>1);

$_SESSION['cart'] = $cart;

}

 

print_r ($cart);

?>

 

There was just some less-than-optimal code because you forgot the ' ' around the array keys.  I added them.  Won't affect the error though, so you'll still need a new session.

Link to comment
Share on other sites

No problem, glad to help.  Please mark this topic as solved.  Also, just for reference (and to maybe reduce the need for another post), when you want to iterate over the cart, here's how you can do it:

 

foreach ($_SESSION['cart'] as $row) {

  echo $row['item_number']; //etc, etc

}

 

That's how the array's formatted.

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.