crashintome1019 Posted December 30, 2012 Share Posted December 30, 2012 (edited) Hello, I'm sorry if I'm not posting this in the correct section; I appologize if I'm not. I'm new to this whole PHP thing, and I am currently building an e-commerce website based on Adam Khoury's video tutorial series on youtube. I'm at a wall here with what I'm trying to do. I've got a drop-box next to the images (products) so that customers can choose which size they will need (the prices will change as well). In the shopping cart page I've got a table with the "Product", "Product Description", "Unit Price", "Quantity", "Total Price" and "Remove" each in their perspective columns. When a customer chooses from the drop-down, I would like that then to send a dollar value to the "Unit Price" column of the cart page and I would also like it to list the size/type of print in the "Product Description" box. I'm not sure how to get it to generate two values like that, or if it's even possible. But here's what I have for code for the product selection page: <form action="cart.php" method="post"> <select name="prints"> <option value="">Select a print size</option> <option value="175.00" name="11x14 Canvas Wrap - $175.00" id="175" >11x14 Canvas Wrap - $175.00 </option> <option value="250.00" name="16x20 Canvas Wrap - $250.00" id="250" >16x20 Canvas Wrap - $250.00 </option> <option value="280.00" name="20x30 Canvas Wrap - $280.00" id="280" >20x30 Canvas Wrap - $280.00 </option> <option value="325.00" name="30x40 Canvas Wrap - $325.00" id="325" >30x40 Canvas Wrap - $325.00 </option> <option value="20.00" name="4x6 Print - $20.00" id="20" >4x6 Print - $20.00 </option> <option value="30.00" name="5x7 Print - $30.00" id="30" >5x7 Print - $30.00 </option> <option value="50.00" name="8x10 Print - $50.00" id="50" >8x10 Print - $50.00 </option> <option value="75.00" name="11x14 Print - $75.00" id="75" >11x14 Print - $75.00 </option> <option value="100.00" name="16x20 Print - $100.00" id="100" >16x20 Print - $100.00 </option> <option value="125.00" name="16x24 Print - $125.00" id="125" >16x24 Print - $125.00 </option> <option value="150.00" name="20x30 Print - $150.00" id="150" >20x30 Print - $150.00 </option> <option value="175.00" name="24x36 Print - $175.00" id="176" >24x36 Print - $175.00 </option> <option value="200.00" name="30x40 Print - $200.00" id="200" >30x40 Print - $200.00 </option> <option value="250.00" name="40x50 Print - $250.00" id="251" >40x50 Print - $250.00 </option> <option value="300.00" name="40x60 Print - $300.00" id="300" >40x60 Print - $300.00 </option> <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> <input type="submit" name="button" id="button" value="Add Selected to Shopping Cart" /></form><br /> I know I will need to add another value in order to get it to generate another value on the "cart.php" page, but I'm just not sure how. My cart.php is an absolute mess which I will have to clean up as well. (PHP boggles my mind). Here's what the code looks like to re-generate the information into that table I was telling you about: <p><?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 4 (if user wants to remove an item from cart) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if (isset($_POST['index_to_remove']) && $_POST['index_to_remove'] != "") { // Access the array and run code to remove that array index $key_to_remove = $_POST['index_to_remove']; if (count($_SESSION["cart_array"]) <= 1) { unset($_SESSION["cart_array"]); } else { unset($_SESSION["cart_array"]["$key_to_remove"]); sort($_SESSION["cart_array"]); } } ?> <?php ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Section 5 (render the cart for the user to view on the page) ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $cartOutput = ""; $cartTotal = ""; $pp_checkout_btn = ''; $product_id_array = ''; if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) { $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>"; } else { // Start PayPal Checkout Button $pp_checkout_btn .= '<form action="[url="https://www.paypal.com/cgi-bin/webscr"]https://www.paypal.com/cgi-bin/webscr[/url]" method="post"> <input type="hidden" name="cmd" value="_cart"> <input type="hidden" name="upload" value="1"> <input type="hidden" name="business" value=" Edited December 30, 2012 by Pikachu2000 Added [nobbc][code] . . . [/code][/nobbc] tags. Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/ Share on other sites More sharing options...
crashintome1019 Posted December 30, 2012 Author Share Posted December 30, 2012 Any help that one can provide would be leaps and bounds past what I currently have. I would appreciate it so much, thank you! Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402128 Share on other sites More sharing options...
Pikachu2000 Posted December 30, 2012 Share Posted December 30, 2012 When posting code, enclose it within the forum's . . . BBCode tags. Moving thread to PHP Coding Help . . . Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402130 Share on other sites More sharing options...
crashintome1019 Posted December 30, 2012 Author Share Posted December 30, 2012 Thanks Pikachu! Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402152 Share on other sites More sharing options...
PFMaBiSmAd Posted December 30, 2012 Share Posted December 30, 2012 Here's some hints for your option menu - 1) You should assign and use an id(identifier) to data items so that you produce and send the minimum amount of information back and forth between the browser and the server. Your option value="..." attributes would simply be the assigned identifier for that option. You would have a data structure (array, database table...) that associates the item identifier to the rest of the information for that item, such as the price, and the description. Also, by have the definition of the options in a data structure, you can let the computer produce the option list, rather than you doing it and fixing all the typo's ... You only need to maintain the defining data structure to add, change, or remove option choices. 2) AFAIK, <option> tags don't have names, so there's no point in the name="..." attribute in the <option> tags. 3) Unless you are specifically using the id="..." attributes, don't put them into the markup. Also, since id attributes must be unique, using the price as the id value would prevent you from correctly repeating that select menu on a page or having any other id with the same price as an existing one. 4) The only place you should actually have and use the price of an item for any calculation is on the server. Any price you output to the browser is for display purposes only. The visitor can submit any value for any form field, so accepting the price from the visitor will mean that you will be selling a lot of things for a lot less they their actual price (or you must now add extra code to make sure that the submitted price is correct.) 5) You should not repeat information - DRY (Don't Repeat Yourself.) For example, each price is now repeated 4 different times (ignoring the things you don't need in the markup.) What happens when you need to change the price of a choice? You must find all the occurrences and modify them. Storing the defining data in a data structure will mean that the price, description, and any other specific information will only exist in one place. 6) You are missing a closing </select> tag. The following is sample code that does the above - <?php // define the print options (array key is the id/identifier) $print_options[1] = array('price'=>175.00,'name'=>"11x14 Canvas Wrap"); $print_options[2] = array('price'=>250.00,'name'=>"16x20 Canvas Wrap"); $print_options[3] = array('price'=>280.00,'name'=>"20x30 Canvas Wrap"); $print_options[4] = array('price'=>325.00,'name'=>"30x40 Canvas Wrap"); $print_options[5] = array('price'=>20.00,'name'=>"4x6 Print"); $print_options[6] = array('price'=>30.00,'name'=>"5x7 Print"); $print_options[7] = array('price'=>50.00,'name'=>"8x10 Print"); $print_options[8] = array('price'=>75.00,'name'=>"11x14 Print"); $print_options[9] = array('price'=>100.00,'name'=>"16x20 Print"); $print_options[10] = array('price'=>125.00,'name'=>"16x24 Print"); $print_options[11] = array('price'=>150.00,'name'=>"20x30 Print"); $print_options[12] = array('price'=>175.00,'name'=>"24x36 Print"); $print_options[13] = array('price'=>200.00,'name'=>"30x40 Print"); $print_options[14] = array('price'=>250.00,'name'=>"40x50 Print"); $print_options[15] = array('price'=>300.00,'name'=>"40x60 Print"); ?> <form action="cart.php" method="post"> <select name="prints"> <option value="">Select a print size</option> <?php foreach($print_options as $option_id=>$option){ echo "<option value='$option_id'>{$option['name']} - $".number_format($option['price'],2)."</option>\n"; } ?> </select> <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /> <input type="submit" name="button" id="button" value="Add Selected to Shopping Cart" /></form><br /> When you submit the above form, you would store the submitted option value (which is the option_id) in the cart for the item it goes with, which I would guess is your pid value. When you display the cart, you would take the option_id value for each item and use it to access the price and description(name) from the data structure where you have the information stored (i.e. the $print_options array in this example.) Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402200 Share on other sites More sharing options...
crashintome1019 Posted December 30, 2012 Author Share Posted December 30, 2012 (edited) Thanks! I'm still struggling with the cart.php but I'll keep working on it. You've been a great help! Edited December 30, 2012 by crashintome1019 Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402207 Share on other sites More sharing options...
crashintome1019 Posted December 31, 2012 Author Share Posted December 31, 2012 Thank you for your help on my question ealier about my PHP shopping cart with the drop-down menu. You gave me this following code: <?php // define the print options (array key is the id/identifier) $print_options[1] = array('price'=>175.00,'name'=>"11x14 Canvas Wrap"); $print_options[2] = array('price'=>250.00,'name'=>"16x20 Canvas Wrap"); $print_options[3] = array('price'=>280.00,'name'=>"20x30 Canvas Wrap"); $print_options[4] = array('price'=>325.00,'name'=>"30x40 Canvas Wrap"); $print_options[5] = array('price'=>20.00,'name'=>"4x6 Print"); $print_options[6] = array('price'=>30.00,'name'=>"5x7 Print"); $print_options[7] = array('price'=>50.00,'name'=>"8x10 Print"); $print_options[8] = array('price'=>75.00,'name'=>"11x14 Print"); $print_options[9] = array('price'=>100.00,'name'=>"16x20 Print"); $print_options[10] = array('price'=>125.00,'name'=>"16x24 Print"); $print_options[11] = array('price'=>150.00,'name'=>"20x30 Print"); $print_options[12] = array('price'=>175.00,'name'=>"24x36 Print"); $print_options[13] = array('price'=>200.00,'name'=>"30x40 Print"); $print_options[14] = array('price'=>250.00,'name'=>"40x50 Print"); $print_options[15] = array('price'=>300.00,'name'=>"40x60 Print");?> <form action="cart.php" method="post"><select name="prints"><option value="">Select a print size</option> <?php foreach($print_options as $option_id=>$option){ echo "<option value='$option_id'>{$option['name']} - $".number_format($option['price'],2)."</option>\n";}?></select><input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" /><input type="submit" name="button" id="button" value="Add Selected to Shopping Cart" /></form><br /> Which makes complete sense. What I am having trouble with now is getting my cart.php to display the selection. I was wondering if you might be able to help out with that code as well? I've spent the majority of the day today trying every different variation I could to get it to work, with zero results. The code I currently have on my cart.php page is as follows: foreach ($_SESSION["cart_array"] as $each_item) { $item_id = $each_item['item_id']; $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1"); while ($row = mysql_fetch_array($sql)) { $product_name = $row["product_name"]; $price = $row["price"]; $details = $row["details"]; } $pricetotal = $price * $each_item['quantity']; $cartTotal = $pricetotal + $cartTotal; setlocale(LC_MONETARY, "en_US"); $pricetotal = money_format("%10.2n", $pricetotal); // Dynamic Checkout Btn Assembly $x = $i + 1; $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '"> <input type="hidden" name="amount_' . $x . '" value="' . $price . '"> <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '"> '; // Create the product array variable $product_id_array .= "$item_id-".$each_item['quantity'].","; // Dynamic table row assembly $cartOutput .= "<tr>"; $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' . $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="100" height="67" border="1" /></td>'; $cartOutput .= '<td>' echo $option_name '</td>'; $cartOutput .= '<td>' echo $option_id '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"> <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" /> <input name="adjustBtn' . $item_id . '" type="submit" value="change" /> <input name="item_to_adjust" type="hidden" value="' . $item_id . '" /> </form></td>'; //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>'; $cartOutput .= '<td>' . $pricetotal . '</td>'; $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>'; $cartOutput .= '</tr>'; $i++; I just can't seem to figure out how to output the values from the product.php page. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402277 Share on other sites More sharing options...
PFMaBiSmAd Posted December 31, 2012 Share Posted December 31, 2012 (edited) Other than the $option_name/$option_id, which you are not setting, I don't see anywhere in your cart data where you have used the 'prints' option value for the item? Have you stored that value in the cart at all? That would be the first step, making sure that your cart contains the data it needs for each item. Edited December 31, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402399 Share on other sites More sharing options...
PFMaBiSmAd Posted January 1, 2013 Share Posted January 1, 2013 Here's someone using the same basic code that you have - http://forums.phpfreaks.com/topic/272404-help-problem-with-some-line-of-codes/ See my comments in post #10 and #12 in that thread, particularly the paragraph at the end of post #10 - you should avoid running database queries inside of loops. Quote Link to comment https://forums.phpfreaks.com/topic/272511-multiple-values-in-a-dropbox-for-shopping-cart/#findComment-1402493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.