Forum Moderators: coopster

Message Too Old, No Replies

creating a list of forms with a loop. which form was filled in?

         

SadSapp

11:27 am on Apr 14, 2008 (gmt 0)

10+ Year Member



I'm stuck on something in a simple shopping cart application. I want to dynamically create a list of each item in the cart showing the picture, the item's name and a form to update the quantity of each item. When I update the quantity of an Item how do I know which item the new quantity is refering to? the problem with the code below is that the $_POST['updatedItem'] refers to the value if $item in the last iteration of the loop. But how do I track which form the new amount has been entered into?
The page is self refering.

cartaction.php:

<?php

if ($_POST['updatedItem'])
{
$itemUpdated = $_POST['updatedItem'];
echo '<div id="header">
<p>Item quantity updated: '.$itemUpdated. '</p>
</div>';
$_SESSION['cart'][$itemUpdated] = $_POST['amount'];
}

foreach ($_SESSION['cart'] as $item => $value){

if ($value > 0){

echo
'<table>
<tr>
<td><img src="images/'.$item.'.jpg" alt="'.$item.'" /></td>
<td><form action = "cartaction.php" method="post">
' .$item .': <input type="text" size=2 name="amount"
value="'.$_SESSION['cart'][$item].'"/>
<input type="submit" value="Update This One Item">
<input type="hidden" name = "updatedItem" value = "'.$item.'">
</td>
</tr>
</table>';
}
}

[edited by: SadSapp at 11:34 am (utc) on April 14, 2008]

d40sithui

3:46 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



hi sadsapp,
how did you organize your items in SESSION['cart']?
here's how i did it. you will need a double array with X by 2 dimensions.
$cart[item][itemId];
$cart[item][itemQuantity];
//example
$cart[0][0]; //this items id
$cart[0][1]; //this items quantity
...
$cart[5][0]; //item id
$cart[5][1]; //item quantity
//this cart has 6 items in it.
//every item in the cart will occupy a 1x2 slot.

so if you have it set up this way, its relatively simple to update the cart. first you'll need to set up your "view cart" form. there is only one form for all your items.

//begin by getting the cart itself.
$cart = $_SESSION['cart']; //gets cart from session

//begin the form
echo "<form action=\"updateCart.php\" method=\"POST\">\n";

//loop through the cart
for($i = 0; $i < sizeof($cart); $i++){
//query item id to get item name from database.
$result = mysql_query("select itemName from itemTable where itemId=".$cart[$i][0]);
$content = mysql_fetch_assoc($result);
$itemName = $content['itemName'];

//print cart contents, this is where setup of your inputs will make it work for you.
echo "<tr><td><input type=\"text\" name=\"item".$cart[$i][0]."\" value=\"".$cart[$i][1]."\">$itemName</td></tr>\n";
//as you can see, the name of this input is the "item"+itemId itself and default value is the quantity. this ensures that further down the loop we will not have another input with the same name and that in the action page, we can run through the POST and update each item based on the input here and the associated quantity.
}

-------------------------------------------------
your action script can look something like this.
//get cart from session.
//run through all your item in the cart and retrieving their item id and quantity
foreach(array_keys($_POST) as $item){
$itemId = str_replace("item","", $item); //item id. since the name of the input is "item" plus the itemId, we'll need to strip off the "item" to get the actual id. i forgot why did this as appose to naming the input only the id itself..
$itemQuantity = $_POST[$item]; //item quantity

//now that you have the itemid and the item quantity, update them.
//first find the position of the item in teh cart array.
$position = null;
for($i = 0; $i < sizeof($cart); $i++){
if($cart[$i][0] == $itemId){
$position = $i;
}
}
//if position found -> update
if(isInt($position)){
$cart[$position][1] = $itemQuantity;
//isInt() is not a core PHP function. i wrote this function to check if the string is a number or not.
}
}//ends foreach
rewrite to session data
$_SESSION['cart'] = $cart;
thats it you're done. as a security measure, when you're looping through the post variables, make sure you verify that the itemid is valid by a query with the db. let me know if all this helps/makes sense.

SadSapp

8:55 pm on Apr 14, 2008 (gmt 0)

10+ Year Member



oh my... thank you so much for your reply. I learned alot from that. But I discovered what the problem was after 2 days of tearing my hair out. Your gonna hate me:

...I didn't close the form tag! So sorry. Thanks so much for taking the time to reply!

d40sithui

2:25 pm on Apr 15, 2008 (gmt 0)

10+ Year Member



haha good. always glad to help.