Web Hosting Talk







View Full Version : duplicate output


tea_clipper
02-22-2008, 12:53 AM
good day ppl,

i got this textbox named quantity for a product with a "$pid", but when i put a number more than "1" example "3", i dnt get that number but instead i have this on my screen $pid = 100,$pid = 100,$pid = 100..

tnx for the input...dnt know how to explain it thouroughly...

dollar
02-22-2008, 12:54 AM
Could you post a snippet of the code that's actually giving you errors? I'm not following you here.

tea_clipper
02-22-2008, 01:15 AM
$cart = $_SESSION['cart'];
$action = $_GET['action'];
switch ($action) {
case 'add':
if ($cart) {
$cart .= ','.$_GET['id'];
} 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;
?>

its actually part of a shopping cart when i "echo the cart" i get the number of $pid instead of its quantity....hope u can shed light this are the codes....

dollar
02-22-2008, 04:04 AM
Thanks for the snippet of the code. So if I'm following things correctly you have a form which has a hidden element named action and with a value of update.

You then have text fields with names like:

qty1
qty2
etc... where the number is the ID of the item in the cart right?

You expect $_SESSION['cart'] to have the quantity and not the list of items?

tea_clipper
02-22-2008, 12:52 PM
<input type="text" name= "qty'.$pid.'" value= "'.$qty.'" maxlength="3" size="3" />

this is the field dynamically generated and you are right i expect quantity and at the same time the quantity of each items pls i need
your help..........tnx

Czaries
02-22-2008, 02:07 PM
Can you do a:

print_r($_SESSION['cart']);

and then post the results so we can see them?

tea_clipper
02-22-2008, 10:31 PM
this is the result (107,107,108,108,109,109)
numbers are primary id and is auto increment....

107 is a product items for a specific drumset
108 another item piano
109 another item guitar

it doubled cause i order 2 of each items, i want the quantity instead of a double productid on the items....

Codebird
02-23-2008, 06:52 AM
you're putting the id in the cart how do you want to get qty!!

in the add you have

$cart .= ','.$_GET['id'];

$cart = $_GET['id'];


and in the update

$id = str_replace('qty','',$key);


you are not storing the qty