Duke555 Posted August 18, 2009 Share Posted August 18, 2009 hello, i am trying to implement a very simple Shopping cart using an arrray as a database and i got stuck. i was wondering if anyone can help. there are a few issues with the code below. 1st of all i was trying to pass an array that acts as a DB ($product[]) as a parameter to the my_cart.php (as opposed to copying and pasting the same array into my_cart.php file). I also tried testing $_POST[] for what values it has. I get only output like: ===============Printing POST[] array===== Array ( [submitted] => 1 [$name] => 0 ) ===== Also another issue is how do i go through the users input (assuming user will enter a number into at least one of the fields as opposed to leaving them as value="0") and the calculate a total price for each and every item user had selected? I assume it has to be done in my_cart.php THANK YOU should be something like?: //assuming $product array was passed down from products.php foreach($product as $name => $description) { // if user selected an item to buy if($POST["$name"]>0) { echo $name."<br>"; echo $description['Price']."<br>"; total += $description['Price']; } } echo "you total for today is :".$total; ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// <?//products.php $product = array('Men\'s Roller Blades'=> array( Price => 69.95, Shipping => 0.10 //shipping as a % of price ), 'Women\'s Roller Blades'=> array( Price => 63.95, Shipping => 0.10 //shipping as a % of price ), 'Helmet'=> array( Price => 34.99, Shipping => 0.05 //shipping as a % of price ), 'Knee Pads'=> array( Price => 22.99, Shipping => 0.08 //shipping as a % of price ), 'Knee and Elbow Pads'=> array( Price => 29.99, Shipping => 0.08 //shipping as a % of price )); // Create a table and a form: echo '<table border="0" width="100%" cellspacing="2" cellpadding="2" align="center"> <tr> <td align="left" width="20%"><b>Product:</b></td> <td align="left" width="15%"><b>Price:</b></td> <td align="left" width="15%"><b>Shipping:</b></td> <td align="left" width="10%"><b>Qty:</b></td> <td align="right" width="15%"><b>Total Price:</b></td> </tr> <form method=POST action="my_cart.php" > <input type="hidden" name="submitted" value="1" /> '; //traversing outer array in this loop //'name' is the 'key' and 'description' is the 'value' of this outer array while (list($name, $description) = each($product)) { //calculating grand total of an item's 'price' and 'shipping' which are stored in the inner //array and are pointed by the keys 'price' and 'shipping' respectively $grand_total = calc_total($description[Price], $description[shipping]); $shipping = $description[shipping] * $description[Price]; ?> <tr> <td align="left"><? echo $name;?></td> <td align="left"><? echo number_format($description[Price], 2);?></td> <td align="left"><? echo number_format($shipping, 2); ?></td> <td align="left"><input type="text" size="3" name="$name" value="0" /></td> <td align="right"><? echo number_format($grand_total, 2);?></td> </tr> <? } // Print the footer, close the table, and the form: ?><tr> <td colspan="4" align="right"><b>Total:</b></td> <!--<td align="right">$' . number_format ($total, 2) . '</td>--> <td align="center">TEST</td> </tr> <tr> <td colspan="6" align="center">TEST</td> </tr> </table><div align="center"><input type="submit" value="SUBMIT" /></div> </form> <FORM ACTION="my_cart.php" NAME="database"> <INPUT TYPE="HIDDEN" NAME="product[]" VALUE="<?=$product?>"> </FORM> <? /* Purpose: function CALCulates a total (incl. tax) price payable by the consumer a given item INPUT: price and shiping for each item OUTPUT: total price which includes tax */ ?> <? function calc_total($price, $shipping) { #tax rate is constant $tax = 0.08; $total_price += $price; $total_tax += $tax * $price; $total_shipping += $shipping * $price; $grand_total = ($total_price + $total_tax + $total_shipping); return $grand_total; } ?> <br> <br> </p> </td> </tr> </TABLE> </div> <? ob_end_flush(); ?> //////////////////////////////////// here is my_cart.php <? //start output buffer to avoid issues with the header() ob_start(); echo "===============Printing POST[] array"; //print_r($MySelect); echo "=====<br>"; print_r($_POST); echo "=====<br>"; print_r($product); ?> <br> <br> <B>Total (including tax and shipping): $<?// echo number_format($grand_total, 2); ?></B> <br> <br> <br> <br> <br> <br> </p> </td> </tr> </TABLE> </div> <? //flush the output buffer AFTER all the header() functions may have been used and thus avoid errors //assoc. with outputting before hearder() functions ob_end_flush(); ?> Link to comment https://forums.phpfreaks.com/topic/170756-stuck-implementing-a-simple-shopping-cart/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.