Flic
08-10-2007, 01:31 AM
Hi,
I am using an array to pass information into a shopping cart.
I want to pass 3 bits of info into the array but only the first one is stored.
Also when I've been sending a different item in its been overwriting the original one instead of having both.
My code is:
<?php
require_once('inc/mysql.class.php'); // Include MySQL class
require_once('inc/global.inc.php'); // Include database connection
require_once('inc/functions.inc.php'); // Include functions
session_start(); // Start the session
$action = $_GET[action]; // The action from the URL
$id = $_GET[id]; // The product id from the URL
$size = $_GET[size];
$quan = $_GET[quan];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET[id];
$cart .= ','.$_GET[size];
$cart .= ','.$_GET[quan].';';
} else {
$cart = $_GET[id];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
$output[] = '<div id="items">';
echo showCart();
$output[] = '</div>';
?>
At the moment I'm just using direct urls to test it.
I have a vague memory from a few years ago that in Java you can define what would be stored in an array and wonder if you can do that in PHP (I can't find anything that says you can) and whether it would help if you can.
Many many thanks, I've been trying to fix this for a few days now!
Flic
I am using an array to pass information into a shopping cart.
I want to pass 3 bits of info into the array but only the first one is stored.
Also when I've been sending a different item in its been overwriting the original one instead of having both.
My code is:
<?php
require_once('inc/mysql.class.php'); // Include MySQL class
require_once('inc/global.inc.php'); // Include database connection
require_once('inc/functions.inc.php'); // Include functions
session_start(); // Start the session
$action = $_GET[action]; // The action from the URL
$id = $_GET[id]; // The product id from the URL
$size = $_GET[size];
$quan = $_GET[quan];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET[id];
$cart .= ','.$_GET[size];
$cart .= ','.$_GET[quan].';';
} else {
$cart = $_GET[id];
}
break;
case 'delete':
if ($cart) {
$items = explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($_GET['id'] != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
$cart = $newcart;
}
break;
case 'update':
if ($cart) {
$newcart = '';
foreach ($_POST as $key=>$value) {
if (stristr($key,'qty')) {
$id = str_replace('qty','',$key);
$items = ($newcart != '') ? explode(',',$newcart) : explode(',',$cart);
$newcart = '';
foreach ($items as $item) {
if ($id != $item) {
if ($newcart != '') {
$newcart .= ','.$item;
} else {
$newcart = $item;
}
}
}
for ($i=1;$i<=$value;$i++) {
if ($newcart != '') {
$newcart .= ','.$id;
} else {
$newcart = $id;
}
}
}
}
}
$cart = $newcart;
break;
}
$_SESSION['cart'] = $cart;
$output[] = '<div id="items">';
echo showCart();
$output[] = '</div>';
?>
At the moment I'm just using direct urls to test it.
I have a vague memory from a few years ago that in Java you can define what would be stored in an array and wonder if you can do that in PHP (I can't find anything that says you can) and whether it would help if you can.
Many many thanks, I've been trying to fix this for a few days now!
Flic