Re: More on removing item from cart
| From: | Vicki | Date: | Fri, 28 Jun 2002 13:59:36 +0000 |
| Subject: | Re: More on removing item from cart | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-104325@lists.php.net to get a copy of this message | ||
Thanks to Jason and ³Sp² for the tip on ³print_r². After all the books I¹ve read, and all the
scrounging through the manual, I¹m amazed I missed that one. It¹s VERY helpful!
Thanks for your comments Dan. I¹ve actually spent quite some time trying to rework the cart because
I agree that the way it¹s written now is strange. However, I hit a wall trying to rework it, and
decided to go back to the same wall I¹ve been banging my head against all week.
I think you understand most of the process with one clarification. When the user unchecks a checkbox
to remove an article from the cart, they click a ³save² button (which triggers the if($save) part
of
the script). The ³checkout² button triggers a separate process that¹s not really involved here.
The
show_cart page is only showing the articles added to the cart and refreshing the cart when articles
are removed and of course setting the session variables for the cart.
I¹ve included the information you requested at the bottom of this message, showing the contents of
the $cart and $checkbox arrays at various stages. The relevant code from the show_cart page is this:
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";
}
if($save) //save button clicked
{
foreach($cart as $artID => $value)
{
if(!isset($yes))
unset($cart[$artID]);
else
$cart[$artID] = $$artID;
}
}
I know the ³foreach² part under if($save) is all wrong. It seems to me that it should be possible
to
write some sort of loop that says ³if the artID (key) for an element in the cart array doesn¹t
match
an artID in the checkbox array, then unset the element with that artID from the cart array.² OR
³only display those elements in the cart array for which an element exists in the checkbox array
that has the same key.²
I¹ve tried a bunch of different ways to do this with no success. I¹m not even sure you can work
with
two arrays in this way in PHP. Couldn¹t you do something like
if ($checkbox[$artID] == $cart[$artID]))
{ then those are the elements that make up the array $cart }
I haven¹t been able to figure this part out. Aargh! Perhaps something is patently obvious in this
code to PHPers. If so, I truly welcome your suggestions, and Dan I really appreciate your looking at
this so closely.
Exasperated, Vicki
PS when I use print_r to display the contents of the arrays, I see a "1" after the array
contents. I
have no idea what that refers to. Any ideas?
___________________________________________________
Here's the code you requested. This is from the View Source window so hopefully you can see
what's
going on more clearly. The first sample is when two new articles have been added to the cart.
I've
included the array info and the relevant html.
<pre> Cart Array at top of show_cart page: Array
(
[3] => 1
[1] => 1
)
1</pre>
<pre> Checkbox Array at top of show_cart page: 1</pre>
<pre> Cart Array after "if($save)": Array
(
[3] => 1
[1] => 2
)
1</pre>
<pre> Checkbox Array after "if($save)": 1</pre>
//
//
//HTML header info deleted here . . .
//
//
<p align="center"><strong>Welcome, User!</strong></p>
<center><a href="show_cat.php?catID=3"><img
src="images/continue-shopping.gif"
alt="Continue Shopping" border=0 height = 25 width =
160></a></center>
<center><a href="http://127.0.0.1/ifiosx/checkout.php">
<img src="images/go-to-checkout.gif" alt="Go To Checkout"
border=0 height = 25 width = 160></a></center></td>
<td class="maincontent" valign="top">
<img src="images/misc/shim.gif" width="160" height="1">
<p><span class="mediumheading"></span></p>
<table border = 0 width = 100% cellspacing = 0>
<form action = show_cart.php method = post>
<tr>
<th bgcolor="#cccccc"> </th>
<th align=left bgcolor="#cccccc">Article</th>
<th bgcolor="#cccccc">Word Count</th></tr>
<tr><td width=5% align = left>
<input type = checkbox name = "checkbox[3]" value = yes
checked></td>
<td align = left><a href = "show_article.php?artID=3">Q &
A</a></td>
<td align = center>235</td>
<tr><td width=5% align = left bgcolor="#f4f4ea">
<input type = checkbox name = "checkbox[1]" value = yes
checked></td>
<td align = left><a href = "show_article.php?artID=1">The Photos
We Keep</a></td>
<td align = center>300</td>
<tr>
<th bgcolor="#cccccc"> </th>
<th align=left bgcolor="#cccccc">Number of Articles = 2</th>
<th align = center bgcolor="#cccccc">535</th>
</tr>
<tr>
<td colspan=3 align = center>
<br>To remove an article from your order, <br>
uncheck the box to the left of the title
and click on the "Save Changes" button.
<br><br>
<input type = hidden name = save value = true>
<input type = image src = "images/save-changes.gif" border = 0
alt = "Save Changes"></td></tr></form></table>
</table>
</body>
</html>
___________________________________
The arrays after "save" button has been clicked (but checkboxes left checked). The keys
are the
correct artID for the articles chosen.
<pre> Cart Array at top of show_cart page: Array
(
[1] => 1
[3] => 1
)
1</pre>
<pre> Checkbox Array at top of show_cart page: Array
(
[1] => yes
[3] => yes
)
1</pre>
<pre> Cart Array after "if($save)": Array
(
[1] =>
[3] =>
)
1</pre>
<pre> Checkbox Array after "if($save)": Array
(
[1] => yes
[3] => yes
)
1</pre>
______________________________________
The arrays after the checkbox for the article with artID==3 has been unchecked. This results in the
cart being emptied entirely and the result "There are no items in your cart."
<pre> Cart Array at top of show_cart page: Array
(
[1] =>
[3] =>
)
1</pre>
<pre> Checkbox Array at top of show_cart page: Array
(
[1] => yes
)
1</pre>
<pre> Cart Array after "if($save)": Array
(
)
1</pre>
<pre> Checkbox Array after "if($save)": Array
(
[1] => yes
)
1</pre>