[PHP] Here's a weird one!

2002-10-27 Thread Steve Jackson
I've been trying for ages to add a form field qty to an array to pass it to
my shopping cart and have finally managed it but not quite.
Please check this cart:
http://www.violasystems.com/shop_testing/

When you add an item using the form text box it doesn't add more than 1 on
the first submit. However if you go back and then add  further items it's
will add the multiple products to the cart? I have three functions which
control this and can't see where I'm going wrong:

Submitting the form:
echo table width ='760' background='images/shopbg.gif'form
action='show_cart.php' method='post'trtd width='380'nbsp;/td;
   echo td width='200' align='right';
   echo bQuantity required: /binput type='text' name='formqty'
value='1' size='3'/td;
   echo td width='180' align='left';
   echo input type='hidden' name='new' value='$ItemCode';
   echo input type='image' src='images/add-to-cart.gif' border ='0' alt
='Add to cart';
   echo /td/tr/form/table;
   echo table width='760' background='images/shopbg.gif'trtd
width='200'nbsp;/tdtdimg src='images/lower.gif'/td/tr/table;

Processing the form:

?
  include ('products_sc_fns.php');
  // The shopping cart needs sessions, so start one
  session_start();
  if($new)
  {
//new item selected
if(!session_is_registered(cart))
{
  $cart = array();
  session_register(cart);
  $items = 0;
  session_register(items);
  $total_price = 0.00;
  session_register(total_price);
}
$qty = $formqty;
 if($cart[$new])
  //$cart[$new]++;
   $cart[$new] = $cart[$new] + $qty;
else
  $cart[$new] = 1;
$total_price = calculate_price($cart);
$items = calculate_items($cart);
  }


  if($save)
  {
foreach ($cart as $ItemCode = $qty)
{
  if($$ItemCode==0)
unset($cart[$ItemCode]);
  else
$cart[$ItemCode] = $$ItemCode;
}
$total_price = calculate_price($cart);
$items = calculate_items($cart);
  }

  do_html_header(Your shopping cart);
  //array de-bug
  //print_r($cart);
  if($cartarray_count_values($cart))
display_cart($cart);
  else
  {
echo table width='760' cellpadding='0'
background='images/shopbg.gif'trtd width='200'nbsp;/tdtdThere are
no items in your cart;
echo /td/tr/table;
  }
  $target = index.php;

  // if we have just added an item to the cart, continue shopping in that
category
  if($new)
  {
$details =  get_product_details($new);
if($details[catid])
  $target = show_cat.php?catid=.$details[catid];
  }
  $path = $PHP_SELF;
  $path = str_replace(show_cart.php, , $path);
  echo table width='760' cellpadding='0'
background='images/shopbg.gif'trtd width='200'nbsp;/tdtd
align='left';
  display_button($target, continue-shopping, Continue Shopping);
  display_button(http://.$SERVER_NAME.$path.checkout.php;,
go-to-checkout, Go To Checkout);
  echo /td/tr/table;
  do_html_footer();
?


And displaying the cart:

function display_cart($cart, $change = true, $images = 1)
{
  // display items in shopping cart
  // optionally allow changes (true or false)
  // optionally include images (1 - yes, 0 - no)

  global $items;
  global $total_price;
  echo table border = 0 width = 760 cellspacing = 0
background='images/shopbg.gif'
form action = show_cart.php method = post
trth width='200'nbsp;/thth colspan = . (1+$images) .
bgcolor=\#146190\font face='Verdana,Arial' size='-1'
color='White'Item/font/th
th bgcolor=\#146190\font face='Verdana,Arial' size='-1'
color='White'Price/font/thth bgcolor=\#146190\font
face='Verdana,Arial' size='-1' color='White'Quantity/font/th
th bgcolor=\#146190\font face='Verdana,Arial' size='-1'
color='White'Total/font/th/tr;

  //display each item as a table row
  $qty = $formqty;
  foreach ($cart as $ItemCode = $qty)
  {
 $product = get_product_details($ItemCode);
echo trtd width='200'nbsp;/td;
if($images ==true)
{
  echo td align = left;
  if (file_exists(images/$ItemCode.jpg))
  {
 $size = GetImageSize(images/.$ItemCode..jpg);
 if($size[0]0  $size[1]0)
 {
   echo img src=\images/.$ItemCode..jpg\ border=0 ;
   echo width = . $size[0]/3 . height =  .$size[1]/3 . ;
 }
  }
  else
 echo nbsp;;
  echo /td;
}
echo td align = left;
echo a href =
\show_products.php?ItemCode=.$ItemCode.\.$product[ItemName]./a
(Code) .$product[ItemCode];
echo /tdtd align = center€ .number_format($product[price], 2);
echo /tdtd align = center;
// if we allow changes, quantities are in text boxes
if ($change == true)
  echo input type='text' name='$ItemCode' value='$qty' size='3';
else
  echo $qty;
echo /tdtd align = center€
.number_format($product[price]*$qty,2)./td/tr\n;
  }
  // display total row
  echo trth width='200'nbsp;/th
  th colspan = . (2+$images) . bgcolor=\#146190\nbsp;/th
  th align = center bgcolor=\#146190\font face='Verdana,Arial'
size='-1' color='White'
  $items
   

Re: [PHP] Here's a weird one!

2002-10-27 Thread @ Edwin
Hello,

I think the problem lies here:

 $qty = $formqty;
  if($cart[$new])
   //$cart[$new]++;
$cart[$new] = $cart[$new] + $qty;
 else
   $cart[$new] = 1;
 $total_price = calculate_price($cart);
 $items = calculate_items($cart);
   }

It seems like you should be adding the $qty on the else part and NOT here:

-- $cart[$new] = $cart[$new] + $qty;

However, I just checked the site and it seems like it's working fine.

I'm using Netscape 7, by the way...

- E


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php