ashton321 Posted January 3, 2009 Share Posted January 3, 2009 I am trying to integrate my shopping cart with paypal, however i can only get one item to work. I beleive it is do to the fact that only the last item in my loop is being past in the $_POST[item_name] variable. In order to send to items all the different parts price name quantity etc need to end with _1 _2 _3 etc. I need to do this for each item in the cart. Here is my current code. But i do not know how to get the numbers to add to the end for each item and still be linked to the correct price and quantity. <?php function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } echo '<form action="http://www.fblaflowers.com/grad/php_paypal/process.php" method="post" id="cart">'; echo '<table width="885" align="center"><tr align="left"><th>Flower</th><th>Unit Price</th><th>Quantity</th><th>Total Price</th></tr>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM flower_inventory WHERE id = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); echo '<tr>'; echo '<td>'.$name.'</td>'; echo '<td>$'.$price.'</td>'; echo '<td>'.$qty.'</td>'; echo '<td>$'.number_format($price * $qty,2).'</td>'; echo '<td><a href="cart.php?action=delete&id='.$id.'" class="r">Remove</a></td>'; $total += $price * $qty; echo '</tr>'; ?> <input type="hidden" name="item_name" id="item_name" value="<?=$name?>"> <input type="hidden" name="amount" id="amount" value="<?=$price?>"> <input type="hidden" name="quantity" id="quantity" value="<?=$qty?>"> <?php } echo '</table>'; echo '<p>Grand total: <strong>$'. number_format($total,2).'</strong></p>'; echo '<div><input type="image" name="submit" src="http://www.fblaflowers.com/images/paypal.gif"></div>'; echo '</form>'; } else { echo '<p>You shopping cart is empty.</p>'; } } Link to comment https://forums.phpfreaks.com/topic/139283-add-increment-based-on-number-of-items-in-an-array-probably-simple/ Share on other sites More sharing options...
ashton321 Posted January 3, 2009 Author Share Posted January 3, 2009 I am thinking that it needs to be done by counting the number of items in the array then using that in a for loop wit ++ until the count of the array? Will this work? Link to comment https://forums.phpfreaks.com/topic/139283-add-increment-based-on-number-of-items-in-an-array-probably-simple/#findComment-728635 Share on other sites More sharing options...
sasa Posted January 3, 2009 Share Posted January 3, 2009 try ... echo '</tr>'; ?> <input type="hidden" name="item_name[]" id="item_name" value="<?=$name?>"> <input type="hidden" name="amount[]" id="amount" value="<?=$price?>"> <input type="hidden" name="quantity[]" id="quantity" value="<?=$qty?>"> <?php } echo '</table>'; ... Link to comment https://forums.phpfreaks.com/topic/139283-add-increment-based-on-number-of-items-in-an-array-probably-simple/#findComment-728789 Share on other sites More sharing options...
ashton321 Posted January 4, 2009 Author Share Posted January 4, 2009 OK i will try it and let you know the results. Thanks for the quick reply. Link to comment https://forums.phpfreaks.com/topic/139283-add-increment-based-on-number-of-items-in-an-array-probably-simple/#findComment-729027 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.