Schlo_50 Posted January 30, 2008 Share Posted January 30, 2008 Hello, I have the following code which echo's out results from my database along with a checkbox (To select an Item) and textbox (To enter the quantity desired) after each output. Here is a screenshot of my page layout: The code which displays the above is this: echo " <p class=main2> <strong>{$ProductName}</strong> - ({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) <span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity\" type=\"text\" size=\"3\" maxlength=\"3\" /></span> </p> "; The idea is that if a user decides to select an item using the checkbox, they must state the quantity in the textbox. Once they have done this they can hit submit and the order details get placed into my database. At the moment i can get the selected items into the database but cannot get the quantities to enter correctly and have no idea what to do. Im finding it hard to get a relationship between the quantities and those items selected. The code to format the selected item numbers is this: (e.g #14, #21, #45) $orderid = $_POST['orderid']; foreach($orderid as $value){ echo "<span class=\"main\">$value, </span>"; $value = implode(",",$_POST['orderid']); } I now need some code to say, "if item number #14 is selected, get the corresponding quanitity". Any help? or is my description of what i want too vague? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/ Share on other sites More sharing options...
rhodesa Posted January 30, 2008 Share Posted January 30, 2008 echo " <p class=main2> <strong>{$ProductName}</strong> - ({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) <span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span> </p> "; foreach($_POST['orderid'] as $id){ echo $id.": ".$_POST['quantity'][$id]."<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453459 Share on other sites More sharing options...
Schlo_50 Posted January 30, 2008 Author Share Posted January 30, 2008 Thanks so much, nearly there! if using your: foreach($_POST['orderid'] as $id){ echo $id.": ".$_POST['quantity'][$id]."<br>"; } How would i get the results into my SQL query? $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$notes','$id')"; Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453481 Share on other sites More sharing options...
rhodesa Posted January 30, 2008 Share Posted January 30, 2008 i don't understand your table. normally, with an order system, you have an Orders table and an OrderItems table. Which is this? Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453490 Share on other sites More sharing options...
Schlo_50 Posted January 30, 2008 Author Share Posted January 30, 2008 Its the table which holds order information. I appreciate i am not doing this in the most appropriate fashion but it has to be done this way. The Order numbers columb in the image below is how the order numbers are stored when a user selects them. I need the quantities to be collected and stored the same way. Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453506 Share on other sites More sharing options...
rhodesa Posted January 30, 2008 Share Posted January 30, 2008 Out of the columns in your query, which one gets the Order Numbers stored into it? I assume Quantity is the column for the quantities. ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453515 Share on other sites More sharing options...
Schlo_50 Posted January 30, 2008 Author Share Posted January 30, 2008 OrderNotes (Apologies for the confusion. Temporary thing!) Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453524 Share on other sites More sharing options...
rhodesa Posted January 30, 2008 Share Posted January 30, 2008 <?php $orderid = array(); $quantity = array(); foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; } $orderid = implode(',',$orderid); $quantity = implode(',',$quantity); $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453528 Share on other sites More sharing options...
Schlo_50 Posted January 30, 2008 Author Share Posted January 30, 2008 hmm..only one quantity is going into the database. If i check item number 12 and 20, and type a quantity of 3 x #12's and 2 x #20's i get: |quantity|ordernotes| | 3 | 12, 20 | The quantity 2 never makes it to the database. I'll show you the whole php file. Beware though, it's quite messy. <form name="the_form" id="the_form" method="post" action="<?php $_SERVER[php_SELF]; ?>"> <?php // connect to access database and find the table 'Product1' $sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg()); // define variables $prevCat=''; while($row = odbc_fetch_array($sql2)) { $Category = $row["CategoryName"]; $ProductId = $row["ProductId"]; $ProductName = $row["ProductName"]; $Price = $row["Price"]; $Type = $row["Type"]; $Habit = $row["Habit"]; $Colour = $row["Colour"]; $Price = $row["Price"]; $Status = $row["Status"]; // find out if category has changed // if so, print it, and it's contents if ($Category != $prevCat) { echo "<p class=title><u>$Category</u></p>"; } echo " <p class=main2> <strong>{$ProductName}</strong> - ({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) <span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span> </p> "; $prevCat = $Category; } ?> <hr /> <div align="center"> <p align="left"><span class="main"><Br /> </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br /> <br /> <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea> <img src="images/eg.jpg" /><br /> <br /> <input name="submit" type="Submit" value="Submit" /> </p> </p> </div> </form> <?php if ($_POST[submit] == "Submit") { $orderid = array(); $quantity = array(); foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; } $orderid = implode(',',$orderid); $quantity = implode(',',$quantity); } ?> <?php $conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!'); $date = date('d m y'); $notes = htmlspecialchars($_POST['ProductNotes']); // execute SQL Statement $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$value','$cust_name','$date','$orderid','$quantity')"; $rs = @odbc_exec($conn,$sql); if (!$rs) { echo ""; } else { echo "<span class=\"main\">The record was successfully inserted.</span>"; } odbc_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453562 Share on other sites More sharing options...
rhodesa Posted January 30, 2008 Share Posted January 30, 2008 After this: <?php if ($_POST[submit] == "Submit") { add this line, fill out the form again, and paste the output here print_r($_POST);exit; Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-453575 Share on other sites More sharing options...
Schlo_50 Posted January 31, 2008 Author Share Posted January 31, 2008 No output now. No confirmation or anything.. Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-454200 Share on other sites More sharing options...
Schlo_50 Posted January 31, 2008 Author Share Posted January 31, 2008 Alright, Got the output now: Array ( [quantity] => Array ( [17] => [14] => [16] => [20] => [12] => [9] => [15] => [21] => 2 [13] => 2 ) [orderid] => Array ( [0] => 21 [1] => 13 ) [ProductNotes] => 1 of Selected [submit] => Submit ) Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-454203 Share on other sites More sharing options...
rhodesa Posted January 31, 2008 Share Posted January 31, 2008 try this.. <form name="the_form" id="the_form" method="post"> <?php //Get products $sql2 = odbc_exec($odbc, "SELECT * FROM Product1 ORDER BY CategoryName") or die (odbc_errormsg()); // define variables $prevCat=''; while($row = odbc_fetch_array($sql2)){ $Category = $row["CategoryName"]; $ProductId = $row["ProductId"]; $ProductName = $row["ProductName"]; $Price = $row["Price"]; $Type = $row["Type"]; $Habit = $row["Habit"]; $Colour = $row["Colour"]; $Price = $row["Price"]; $Status = $row["Status"]; // find out if category has changed // if so, print it, and it's contents if($Category != $prevCat){ echo "<p class=title><u>{$Category}</u></p>"; } echo " <p class=main2> <strong>{$ProductName}</strong> - ({$Type} , {$Habit} , {$Colour} , {$Status}, Item#{$ProductId}) <span class=address1>£{$Price} <input name=\"orderid[]\" value=\"{$ProductId}\" type=\"checkbox\"/> <input name=\"quantity[{$ProductId}]\" type=\"text\" size=\"3\" maxlength=\"3\" /></span> </p> "; $prevCat = $Category; } ?> <hr /> <div align="center"> <p align="left"><span class="main"><Br /> </span><span class="title">Order Quantity-</span><span class="main"> If you would like to order more than one batch of any of the above, please state in the textbox below the Item# and quantity you wish to order in the format shown:</span> <span class="main">(If you do not wish to order more than one of any item do not fill in the textbox below.)</span><br /> <br /> <textarea name="ProductNotes" cols="70" rows="10">1 of Selected</textarea> <img src="images/eg.jpg" /><br /> <br /> <input name="submit" type="Submit" value="Submit" /> </p> </p> </div> </form> <?php if($_POST['submit'] == "Submit"){ //Connect to database $conn = odbc_connect('CentralProduct', 'root', '') or die('Could not Connect to ODBC Database!'); $orderid = array(); $quantity = array(); foreach($_POST['orderid'] as $id){ $orderid[] = $id; $quantity[] = $_POST['quantity'][$id]; } $orderid = implode(',',$orderid); $quantity = implode(',',$quantity); $date = date('d m y'); $notes = htmlspecialchars($_POST['ProductNotes']); //execute SQL Statement $sql = "INSERT INTO Order1 " . "(ProductNotes,CustomerName,OrderDate,OrderNotes,Quantity) VALUES ('$notes','$cust_name','$date','$orderid','$quantity')"; $rs = @odbc_exec($conn,$sql); if(!$rs) echo "Error: ".odbc_errormsg(); else echo "<span class=\"main\">The record was successfully inserted.</span>"; odbc_close($conn); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/88572-quantity/#findComment-454379 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.