midjam Posted September 25, 2010 Share Posted September 25, 2010 Hi guys, forum noob here. I'm very new to php and i'm trying to build a simple food order shopping cart. I'm using DW as a crutch at the moment just to get a feeling of how php works. I have a page where a user can select food extras to add to thier food item using checkboxes. heres the recordset for getting extras. $colname_rs_addedextras = "1"; if (isset($_GET['idfooditem'])) { $colname_rs_addedextras = $_GET['idfooditem']; } mysql_select_db($database_dbconnect, $dbconnect); $query_rs_addedextras = sprintf("SELECT extra.idextra, extra.extraName, extra.extraPrice, addedextras.extraFree FROM addedextras, extra WHERE fooditemID = %s AND addedextras.extraID = extra.idextra", GetSQLValueString($colname_rs_addedextras, "int")); $rs_addedextras = mysql_query($query_rs_addedextras, $dbconnect) or die(mysql_error()); $row_rs_addedextras = mysql_fetch_assoc($rs_addedextras); $totalRows_rs_addedextras = mysql_num_rows($rs_addedextras); Here is the code i'm strugling with. // Submit to Order Form ///////////////////////////// if (isset($_POST["addtoorderButton"])) { mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error()); if ($_POST['checkbox'] != NULL) { $cart_id = mysql_insert_id(); foreach($_POST['checkbox'] as $key => $value) { $extraName = $row_rs_addedextras['extraName']; $extraPrice = $row_rs_addedextras['extraPrice']; $extraFree = $row_rs_addedextras['extraFree']; //echo $key; mysql_query("INSERT INTO cartextras(cartID, cookieID, extraName, extraID, extraPrice, extraFree) VALUES('$cart_id', '$cookieID', '$extraName', '$key', '$extraPrice', '$extraFree') ") or die(mysql_error()); } } } The code kinda works, I just can't figure out how to add the extraPrice and extraFree also. Any kind of guidence would be very apprieated as I have been trying to figure this out for a couple of days now, wanted to figure it out by myself because I think to learn faster that way. Quote Link to comment Share on other sites More sharing options...
midjam Posted September 25, 2010 Author Share Posted September 25, 2010 couldn't edit my post :-\ posted the wrong code sorry here is my updated one. // Submit to Order Form ///////////////////////////// if (isset($_POST["addtoorderButton"])) { mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error()); if ($_POST['checkbox'] != NULL) { $cart_id = mysql_insert_id(); foreach($_POST['checkbox'] as $key => $value) { //echo $key; mysql_query("INSERT INTO cartextras(cartID, cookieID, extraName, extraID, extraPrice, extraFree) VALUES('$cart_id', '$cookieID', '".$row_rs_addedextras['extraName']."', '$key', '".$extraPrice = $row_rs_addedextras['extraPrice']."', '".$row_rs_addedextras['extraFree']."') ") or die(mysql_error()); } } } I know it has something to do with the checkboxes and how the foreach loops through each of them, but i'm too inexperienced to work it out myself. Quote Link to comment Share on other sites More sharing options...
kickstart Posted September 25, 2010 Share Posted September 25, 2010 Hi Basic code looks OK (assuming you are not looping around the data returned by the select). In your second post you have an assignment to a field instead of using that field in the SQL, but you do not have that problem in the first post so I assume it is a typo. If it isn't looping around correctly that suggests that $_POST['checkbox'] might not be an array. Try putting print_r($_POST['checkbox']); before the loop to check the contents of the array. All the best Keith Quote Link to comment Share on other sites More sharing options...
midjam Posted September 25, 2010 Author Share Posted September 25, 2010 Thanks for your reply, yes its looping around whether or not the checkbox is checked or not. I have been playing around with the code and have nearly got it. I'm trying to filter whether the checkbox has been checked and if so record the extra name, extra price and whether the extra is free or not. Heres what I have, but it's not quite there. ///////////////////////////////////////////////////// // Submit to Order Form ///////////////////////////// if (isset($_POST["addtoorderButton"])) { mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error()); if ($_POST['checkbox'] != NULL) { $cart_id = mysql_insert_id(); echo "Checkboxes: "; print_r ($_POST['checkbox']); foreach($_POST['hextraFree'] as $key2) { $extraFree = $key2; echo "extra free : "; echo $extraFree; echo "<br />"; foreach($_POST['hextraName'] as $key3 => $value3) { $extraName = $value3; echo "extra name : "; echo $extraName; echo "<br />"; foreach($_POST['hextraPrice'] as $key4 => $value4) { $extraPrice = $value4; echo "extra price : "; echo $extraPrice; echo "<br />"; foreach($_POST['checkbox'] as $key => $value) { //echo $key; mysql_query("INSERT INTO cartextras(cartID, cookieID, extraName, extraID, extraPrice, extraFree) VALUES('$cart_id', '$cookieID', '$extraName', '$key', '$extraPrice', '$extraFree') ") or die(mysql_error()); } } } } } } This is the output, it does insert to the database fine, just too many. Checkboxes: Array ( [1] => on [6] => on ) extra free : 0 extra name : Bacon extra price : 0.80 extra price : 0.10 extra name : Lettice extra price : 0.80 extra price : 0.10 extra free : 1 extra name : Bacon extra price : 0.80 extra price : 0.10 extra name : Lettice extra price : 0.80 extra price : 0.10 I can't figure out how to get it too record extraName, extraPrice, extraFree then insert into database if the checkbox is checked. Hope this makes sense Quote Link to comment Share on other sites More sharing options...
midjam Posted September 25, 2010 Author Share Posted September 25, 2010 Been playing with the code again and so far, it checks if the checkbox is checked and if so inserts the records, but it's only inserting the FIRST extraName, extraPrice and extraFree record into each row. ///////////////////////////////////////////////////// // Submit to Order Form ///////////////////////////// if (isset($_POST["addtoorderButton"])) { mysql_query("INSERT INTO cart(cookieID, customerID, fooditemID, foodItemName, foodItemPrice) VALUES('$cookieID', '$customerID', '$fooditemID', '$foodItemName', '$foodItemPrice' ) ") or die(mysql_error()); if ($_POST['checkbox'] != NULL) { $cart_id = mysql_insert_id(); echo "Checkboxes: "; print_r ($_POST['checkbox']); foreach($_POST['hextraFree'] as $key2) { $extraFree = $key2; echo "extra free : "; echo $extraFree; echo "<br />"; break; } foreach($_POST['hextraName'] as $key3 => $value3) { $extraName = $value3; echo "extra name : "; echo $extraName; echo "<br />"; } foreach($_POST['hextraPrice'] as $key4 => $value4) { $extraPrice = $value4; echo "extra price : "; echo $extraPrice; echo "<br />"; } foreach($_POST['checkbox'] as $key => $value) { //echo $key; mysql_query("INSERT INTO cartextras(cartID, cookieID, extraName, extraID, extraPrice, extraFree) VALUES('$cart_id', '$cookieID', '$extraName', '$key', '$extraPrice', '$extraFree') ") or die(mysql_error()); } } } ///////////////////////// The output is now: Checkboxes: Array ( [1] => on [6] => on ) extra free : 0 extra name : Bacon extra name : Lettice extra price : 0.80 extra price : 0.10 but it still shows all extras if checked or not. heres the repeat region if it helps. <table width="100%" border="0" cellspacing="6" cellpadding="6"> <tr> <td width="6%"><img src="../images/foodimages/<?php echo $row_rs_fooditem['foodItemImage']; ?>" width="250" height="150" /></td> <td width="94%"><?php do { ?> <table width="200" border="0" cellspacing="0" cellpadding="0"> <tr> <td><input <?php if (!(strcmp($row_rs_addedextras['extraFree'],true))) {echo "checked=\"checked\"";} ?> type="checkbox" name="checkbox[<?php echo $row_rs_addedextras['idextra']; ?>]" id="checkbox" /> <label for="checkbox[]"></label></td> <td><?php echo $row_rs_addedextras['extraName'];?></td> <td>£ <?php echo $row_rs_addedextras['extraPrice']; ?></td> </tr> <tr> <td colspan="3"> <input name="hextraName[<?php echo $row_rs_addedextras['extraName']; ?>]" type="text" id="hextraName[]" value="<?php echo $row_rs_addedextras['extraName']; ?>" /> <label for="hextraPrice[]"></label> <input name="hextraPrice[<?php echo $row_rs_addedextras['extraPrice']; ?>]" type="text" id="hextraPrice[]" value="<?php echo $row_rs_addedextras['extraPrice']; ?>" /> <input name="hextraFree[<?php echo $row_rs_addedextras['extraFree']; ?>]" type="text" id="hextraFree[]" value="<?php echo $row_rs_addedextras['extraFree']; ?>" /></td> </tr> </table> <?php } while ($row_rs_addedextras = mysql_fetch_assoc($rs_addedextras)); ?> </td> </tr> </table> 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.