kerry Posted October 10, 2011 Share Posted October 10, 2011 I have used the shopping cart as in the book, but I have a problem. If there is one product in the cart the shipping is applied once (even if there are 2 of this item) the problem is if I add another product, it adds the shipping twice. Code is below: <?php #script together. This combines addtocart and view cart. $_SESSION['total'] = $_POST['total']; $_SESSION['product_name'] = $_POST['product_name']; $page_title = 'Add to cart'; if (isset ($_GET['pid']) && is_numeric($_GET['pid']) ) { $pid = (int) $_GET['pid']; if (isset($_SESSION['cart'][$pid])) { $_SESSION['cart'][$pid]['quantity']++; }else{ //new product require_once ('../mysqli_connect.php'); $q="SELECT price FROM product WHERE product_id=$pid"; $r=mysqli_query ($dbc, $q); if (mysqli_num_rows($r) == 1) { list($price) = mysqli_fetch_array ($r, MYSQLI_NUM); $_SESSION['cart'][$pid] = array ('quantity' => 1, 'price' => $price); }else{ echo '<div align="center">Your basket has been updated</div>'; } } }else{ //no print ID echo '<div align="center"> Your basket has been updated</div>'; } if (isset($_POST['submitted'])) { foreach ($_POST['qty'] as $k => $v) { $pid = (int) $k; $qty = (int) $v; if ($qty ==0) { // Delete. unset ($_SESSION['cart'][$pid]); } elseif ($qty >0 ) { // Change quantity. $_SESSION['cart'][$pid]['quantity'] = $qty; } } // End of FOREACH. } // End of SUBMITTED IF. // Display the cart if it's not empty... if (!empty($_SESSION['cart'])) { $q="SELECT product_id, description, price, product_name FROM product WHERE product_id IN ("; foreach ($_SESSION['cart'] as $pid => $value) { $q .= $pid . ','; } $q=substr($q, 0, -1) . ') ORDER BY product_name ASC'; $r = mysqli_query ($dbc, $q); // Create a form and a table: echo '<form action="basket.php" method="post"> <table border="0" width="90%" cellspacing="3" cellpadding="3" align="center"> <tr> <td align="left" width="30%"><b>Description</b></td> <td align="left" width="30%"><b>Product Name</b></td> <td align="right" width="10%"><b>Price</b></td> <td align="center" width="10%"><b>Qty</b></td> <td align="right" width="10%"><b>Total Price</b></td> </tr> '; $total =0; // Total cost of the order. while ($row = mysqli_fetch_array ($r, MYSQLI_ASSOC)) { $subtotal = $_SESSION['cart'][$row['product_id']]['quantity'] * $_SESSION['cart'][$row['product_id']]['price']; $postage=1.00; $total += ($postage + $subtotal); echo "\t<tr> <td align=\"left\">{$row['description']}</td> <td align=\"left\">{$row['product_name']}</td> <td align=\"right\">£{$_SESSION['cart'][$row['product_id']]['price']}</td> <td align=\"center\"><input type=\"text\" size=\"3\" name=\"qty[{$row['product_id']}]\" value=\"{$_SESSION['cart'][$row['product_id']]['quantity']}\" /></td> <td align=\"right\">£" . number_format ($subtotal, 2) . "</td> </tr>\n"; } // End of the WHILE loop. $_SESSION['total']=$total; $_SESSION['order_id'] =$order_id; $pn = $_SESSION['product_name']; mysqli_close($dbc); // Close the database connection. echo '<tr> <td colspan="4" align="right"><b> Postage:</b></td><br/> <td align="right">£' . number_format ($postage, 2) . '</td> <br/> <br/> <br/> <br/> <td colspan="4" align="right"><b>Total:</b></td> <td align="right">£' . number_format ($total, 2) . '</td> </tr> </table><p align="left">Enter a quantity of 0 to remove an item. <div align="left"><input type="submit" name="submit" value="Update" /></div> <input type="hidden" name="submitted" value="TRUE" /> <p align="center"><a href="logincustomers.php"><img src="checkout.gif" width="83" height="27" border="0"/></a></p>'; } else { echo '<p>Your cart is currently empty.</p>'; } ?> Link to comment Share on other sites More sharing options...
Larry Posted October 10, 2011 Share Posted October 10, 2011 That's because in your while loop you add the postage to the total every time. Link to comment Share on other sites More sharing options...
kerry Posted October 11, 2011 Author Share Posted October 11, 2011 I have tried to remove this from the while loop, but then it does not work. Can you please advise on how I can create the postage outside of the loop so it works. Thanks Link to comment Share on other sites More sharing options...
Larry Posted October 12, 2011 Share Posted October 12, 2011 Just add the postage after the while loop. } // End of while loop. $total += $postage; Link to comment Share on other sites More sharing options...
Recommended Posts