nicolasxr Posted November 5, 2008 Share Posted November 5, 2008 Hello everyone, I am new to this forum yay! ok, I got a question on this shopping cart. how can i check stock with the database in this function: <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p><center>You have no items in your shopping cart</center></p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<center>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></center>'; } } Here is the full code with my attempt, its half right and half wrong. people cant add more than what I got, but the item display shows whatever number was added. <?php function writeShoppingCart() { $cart = $_SESSION['cart']; if (!$cart) { return '<p><center>You have no items in your shopping cart</center></p>'; } else { // Parse the cart session variable $items = explode(',',$cart); $s = (count($items) > 1) ? 's':''; return '<center>You have <a href="cart.php">'.count($items).' item'.$s.' in your shopping cart</a></center>'; } } 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; } $output[] = '<form action="cart.php?action=update" method="post" id="cart">'; $output[] = '<table border=0 align="center" CLASS="cart_table" width="600px" >'; $output[] = '<tr>'; $output[] = '<td bgcolor="#c2b8ae"><b>Item</b></td>'; $output[] = '<td bgcolor="#c2b8ae"><b>Unit Price</b></td>'; $output[] = '<td bgcolor="#c2b8ae"><b>Quantity</b></td>'; $output[] = '<td bgcolor="#c2b8ae"><b>Total</b></td>'; $output[] = '<td bgcolor="#c2b8ae"><b>Remove?</b></td>'; $output[] = '</tr>'; foreach ($contents as $id=>$qty) { $sql = 'SELECT * FROM product WHERE productID = '.$id; $result = $db->query($sql); $row = $result->fetch(); extract($row); $output[] = '<tr> '; $output[] = '<td>'.$name.'</td>'; $output[] = '<td>$'.$price.'</td>'; if ($qty > $in_stock) { $qty = $in_stock; $output[] = '<td><input type="text" align="right" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; } else { $output[] = '<td><input type="text" align="right" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>'; } $output[] = '<td>$'.($price * $qty).'</td>'; $total += $price * $qty; $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r"><img src="images/remove.gif" border=0></a></td>'; $output[] = '</tr>'; $shipping = "<b>FREE<b>"; $newtotal = $total; } $output[] = ' '; $output[] = '<tr height=20px><td></td><td></td><td></td><td></td><td></td></tr>'; $output[] = '<tr><td></td><td></td><td></td><td bgcolor="#c2b8ae"><strong>Sub-total:</td><td bgcolor="#c2b8ae"><font color="black">$'.$total.'</font></strong></td></tr>'; $output[] = '<tr><td></td><td></td><td></td><td bgcolor="#c2b8ae"><strong>Shipping:</td><td bgcolor="#c2b8ae"><font color="black">'.$shipping.'</font></strong></td></tr>'; $output[] = '<tr><td></td><td></td><td></td><td bgcolor="#c2b8ae"><strong>Total:</td><td bgcolor="#c2b8ae"><font color="black"><b>$'.$newtotal.'</b></font></strong></td></tr>'; $output[] = '<tr><td></td><td></td><td></td><td></td><td></td></tr>'; $output[] = '<tr><td></td><td></td><td></td><td bgcolor="#c2b8ae"><div><button type="submit">Update cart</button></div></td><td bgcolor="#c2b8ae"><div><button type="submit">Checkout</button></div></td></tr>'; $output[] = '</form></table>'; } else { $output[] = '<p><center>You shopping cart is empty.</center></p>'; } return join('',$output); } ?> Link to comment https://forums.phpfreaks.com/topic/131440-help-and-hello/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.