NFD Posted July 24, 2008 Share Posted July 24, 2008 Hi, Got a bit of an interesting problem here regarding quantity fields on a dynamically generated form in a shopping cart. On the view product page, what I am trying to do is have it so there is a form a customer can fill out specifying a quantity for each option available. All options relate directly to the product being viewed. e.g. 1 x red colored widget 5 x black colored widgets 0 x white colored widgets 2 x blue colored widgets Here is the query I am currently running to retrieve the available options for a product: if($prodArray == TRUE && $prodArray[0]['subOptionsStatus']==1){ $subOptionsQuery = "SELECT * FROM ".$glob['dbprefix']."inventory_sub_options WHERE subOptionStatus = 1 AND subOptionProductId = ".$db->mySQLSafe($_GET['productId'])." ORDER BY subOptionIdSortId "; $subOptionsResults = $db->select($subOptionsQuery); } if($subOptionsResults == TRUE){ for ($i=0; $i<count($subOptionsResults); $i++){ $view_prod->assign("VAL_SUB_OPTION_ID", $subOptionsResults[$i]['subOptionId']); $view_prod->assign("VAL_SUB_OPTION_GENDER", $subOptionsResults[$i]['subOptionGender']); $view_prod->assign("VAL_SUB_OPTION_COLOR", $subOptionsResults[$i]['subOptionColor']); $view_prod->assign("VAL_SUB_OPTION_STYLE", $subOptionsResults[$i]['subOptionStyle']); $view_prod->assign("VAL_SUB_OPTION_SIZE", $subOptionsResults[$i]['subOptionSize']); $view_prod->assign("VAL_SUB_OPTION_PRICE", priceFormat($subOptionsResults[$i]['subOptionPrice'])); $view_prod->parse("view_prod.prod_true.sub_options.sub_option_loop"); } $view_prod->parse("view_prod.prod_true.sub_options"); } That so far does exactly what I want. Then there is the html for the form itself. Here is what I currently have: <table cellpadding="0" cellspacing="0" width="100%" border="1"> <tr> <td align="center" colspan="6"><strong>Title Goes Here</strong></td> </tr> <tr> <td align="center"><strong>Gender</strong></td> <td align="center"><strong>Color</strong></td> <td align="center"><strong>Style</strong></td> <td align="center"><strong>Size</strong></td> <td align="center"><strong>Price</strong></td> <td align="center"><strong>Qty</strong></td> </tr> <!-- BEGIN: sub_option_loop --> <tr> <td align="center">{VAL_SUB_OPTION_GENDER}</td> <td align="center">{VAL_SUB_OPTION_COLOR}</td> <td align="center">{VAL_SUB_OPTION_STYLE}</td> <td align="center">{VAL_SUB_OPTION_SIZE}</td> <td align="center">{VAL_SUB_OPTION_PRICE}</td> <td align="center"> <input name="subOptionQuantity[]" type="text" value="0" size="2" class="textbox" style="text-align:center;" /> <input type="hidden" name="multiadd" value="{VAL_SUB_OPTION_ID}" /> </td> </tr> <!-- END: sub_option_loop --> <input type="hidden" name="multiAddMaster" value="{PRODUCT_ID}" /> </table> Now I don't think that is quite correct when it comes to the fields themselves, but visually it achieves what I want to be displayed. Finally, there is the code to run when the form itself gets submitted. The standard code for just a single product is like this: if(isset($_POST['add']) && $_POST['add']>0) { // add product to the cart if($_POST['quan']>0){ $quantity = ceil($_POST['quan']); } else { $quantity = 1; } } As above, that is simply how it adds a single product based off quantity entered. Obviously it does more after all that, but i am just trying to get the initial stage working first. My 2 questions are: 1. What changes need to be made to the HTML code I am currently using? 2. How do I do the add quantity for each one specified in the form using the above as an example? If the quantity is at 0, I want it ignored. If the quantity is greater than 0, its to be added as its own entry. Once I get past this initial stage, I am hoping it ends up basically adding each as its own product to the cart. Any help provided getting started here would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Barand Posted July 24, 2008 Share Posted July 24, 2008 if you have <input name="subOptionQuantity[{VAL_SUB_OPTION_ID}]" type="text" value="0" size="2" class="textbox" style="text-align:center;" /> To process foreach ($_POST['subOptionQuantity'] as $id => $qty) { // now you have id and qty to process as required } Quote Link to comment Share on other sites More sharing options...
NFD Posted July 24, 2008 Author Share Posted July 24, 2008 That is exactly what I needed! Thank you so very much! Now I do indeed have an id and the quantity to work with for each option. Again, thank you, very much appreciated Quote Link to comment 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.