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
https://forums.phpfreaks.com/topic/118174-solved-arrays-for-cart/
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?

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.

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.

Archived

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

×
×
  • 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.