Re: More on removing item from cart
| From: | Vicki | Date: | Wed, 26 Jun 2002 15:01:31 +0000 |
| Subject: | Re: More on removing item from cart | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-103976@lists.php.net to get a copy of this message | ||
Dan:
Thanks very much for the detailed reply. I took most of this code from Welling and Thomson's
book
"PHP and MySQL Development," (chpt. 25) but I'm trying to modify it to fit my
purposes. The html is
mostly theirs, and it's not the way I would write it, but it's working on my test machine.
I plan to
clean it up and do cross-browser testing once the scripts work.
I agree completely that the problem is most likely in the foreach line of the following code.
if($save)
{
foreach($cart as $artID => $value)
{
if(empty($value))
unset($cart[$artID]);
else
$cart[$artID] = $$artID;
}
You asked about the structure of the cart, and here's another problem area. One of the greatest
difficulties I've had learning PHP (my first attempt at any scripting/programming language, in
case
you hadn't noticed), is understanding array structures.
The cart is created by the show_cart page when items are added to it. Here's the relevant code
(which immediately precedes the "if($save)" snippet sent in the previous message:
<?php
include ('article_sc_fns.php');
session_start();
if($new)
{
//new item selected
if(!session_is_registered("cart"))
{
$cart = array();
session_register("cart");
$article_qty = 0;
session_register("article_qty");
$total_words = 0;
session_register("total_words");
}
if($cart[$new])
$cart[$new]++;
else
$cart[$new] = 1;
When the article is added to the shopping cart the variable is passed to the "show_cart"
page
through the URL ("show_cart.php?new=$artID") The original code was written to allow for
changing
quantities of items in the cart, which is irrelevant for me, but I've left the code as is
because it
doesn't seem to interfere with any other code and is necessary for adding items to the cart.
I'm checking the cart structure by printing the following code inserted right after the
"if($save);"
line (is this the best way to check on the contents of an array?)
while (list($key,$value) = each($cart))
{
print "Cart = $key: $value<BR>";
}
This returns "article ID number (an integer) : 1" when new
and "article ID : yes" after the save button has been clicked.
I tried naming the checkbox "want[$artID]" but as far as I can tell the $want array and
the $cart
array are two separate entities and none of the code I've tried seems to work on both, even
when
they have the same key ($artID). For example, when I revised the code you sent earlier to
while (list($artID,) = each ($_POST['want']))
$cart[$artID];
it didn't work. Obviously, something else could be interfering.
Re: the line of code you thought odd and asked about:
else
$cart[$artID] = $$artID;
I thought it was odd too, and since this is acquired code, I can't explain it. I assumed that
it
just resets the artID to its original value.
I'm afraid this message is way too long, and I apologize if it's too much. I hesitated
posting
questions in the first place because the interaction of the code seemed fairly complex, but I'm
also
quite desperate.
Once again, many thanks for your patience and attention.
Vicki