naima69 Posted April 9, 2007 Share Posted April 9, 2007 Hi. I have a ordersystem where data is stored in MySql database. My problem is to loop actual orders and insert them into the database table. Somebody told me to use the "foreach" looping, but I don't understand HOW. Here's an examle to illustrate my order system: ---------------- "listproducts.php" ------------------------------------- <? //My Sql selecting query mysql_connect($host,$user,$password); @mysql_select_db($database) or die("Unable to select database"); $query= "SELECT * FROM product"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); // Looping the products using "while" $i=0; while ($i < $num) { $prod_id=mysql_result($result,$i,"prod_id"); $prod_name=mysql_result($result,$i,"prod_name"); echo "<form action=\"order.php\" method=\"post\">"; echo "<input type=\"hidden\" name=\"prod_nr\" size=\"0\" value=\"$prod_id\">"; echo "<input type=\"text\" size=\"18\" value=\"$prod_name\">"; echo "<input type=\"text\" name=\"prod_qty\" size=\"2\" value=\"0\" class=\"qty\">"; //as you se here, "qty" is set to "0" instead of an empty field echo "<input type=\"reset\" value=\"Reset\">"; echo "<input type=\"submit\" value=\"Order\">"; echo "</form>"; $i++; } ?> //End of "listproducts.php" ---------------- "order.php" ------------------------------------- <? $prod_id=$_POST['prod_id']; $prod_name=$_POST['prod_name']; $prod_qty=$_POST['prod_qty']; //Now, the above $_POST is the information I have from the page "listproducts.php" //I want to put into a form the values from where my customer has set the "$prod_qty" larger than "0", //which is the actually ordered items //How can I do this through a loop? Can someone give me a code for this, relating to my example? ?> //Then I would like to loop it one more time, but now for the purpose to INSERT the ordered items into an //"order_items" table in my database (In my real ordersystem I have a variable "order_num" used to register an order in //an "order" table. This order_num will repeat for every row of ordered items.) //Can you help me with the code for looping the INSERT query also? //End of "order.php" And is it possible to do this a simple way? Thank you and lots of kisses for all help. Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/ Share on other sites More sharing options...
papaface Posted April 9, 2007 Share Posted April 9, 2007 foreach ($_POST as $key=>$value) { if ($key == "prod_qty" && $value > 0) { echo $value; } } That will look through the $_POST array keys and find the one named prod_qty and if it is more than 0 it will echo the value of that key out. I'm a bit rusty on foreach's so there may be an error in there. But try it and see what you get. Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-224987 Share on other sites More sharing options...
naima69 Posted April 9, 2007 Author Share Posted April 9, 2007 Thank you for trying to help me. I've tried it, but it leaves a blank page. Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-225012 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2007 Share Posted April 9, 2007 The first problem I see is that you're creating a new form for each record in your data base, you need to use one form that contains all of the records. Also instead of using the mysql_result() function it's much easier to use one of the mysql_fetch functions: <?php mysql_connect($host,$user,$password); @mysql_select_db($database) or die("Unable to select database"); $query= "SELECT * FROM product"; $result=mysql_query($query); $num=mysql_num_rows($result); if ($num > 0) { echo '<form action="order.php" method="post">'; while ($rw = mysql_fetch_array($result)) { echo '<input type="text" size="18" value="' . $rw['prod_name'] . '" name="product_name[' . $rw['prod_id'] . ']">'; echo '<input type="text" name="prod_qty[' . $rw['prod_id'] . ']" size="2" value="0" class="qty"><br>'; } echo '<input type="reset" value="Reset">'; echo '<input type="submit" value="Order" name="submit">'; echo "</form>"; } ?> In your processing script you would do something like: <?php if (isset($_POST['submit'])) { foreach ($_POST['product_name'] as $key => $val) { echo 'Product ID: ' . $key . ', Product Name: ' . $val . ', Quantity ordered: ' . $_POST['prod_qty'][$key] . '<br>'; // for debugging if ($_POST['prod_qty'][$key] != 0) { // // your code for ordering // } } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-225015 Share on other sites More sharing options...
naima69 Posted April 9, 2007 Author Share Posted April 9, 2007 Thank you, Ken. This was really helpful. The only thing now, is that I get about 122 lines saying: Product ID: 00135, Product Name: Thermorullar, Quantity ordered: 0 Product ID: 00136, Product Name: Thermorullar, Quantity ordered: 0 Product ID: 00058, Product Name: Thermorullar, Quantity ordered: 0 Product ID: 00059, Product Name: Thermorullar, Quantity ordered: 0 And one little line in the middle somewhere saying: Product ID: 00065, Product Name: Thermorullar, Quantity ordered: 1 I tried to put in an "else" statement below the processingscript you wrote for me, but I only received an error. Do you know how to not display the not-ordered items? Thank you Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-225085 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2007 Share Posted April 9, 2007 Move the echo statement to within the "if" block: <?php foreach ($_POST['product_name'] as $key => $val) { if ($_POST['prod_qty'][$key] != 0) { echo 'Product ID: ' . $key . ', Product Name: ' . $val . ', Quantity ordered: ' . $_POST['prod_qty'][$key] . '<br>'; // for debugging // // your code for ordering // } } ?> Ken Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-225093 Share on other sites More sharing options...
Wildbug Posted April 9, 2007 Share Posted April 9, 2007 Do you know how to not display the not-ordered items? SELECT * FROM product WHERE qty > 0 Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-225105 Share on other sites More sharing options...
naima69 Posted April 16, 2007 Author Share Posted April 16, 2007 Hi. Thank you for your help so far. I finally got the first step solved with your help Link to comment https://forums.phpfreaks.com/topic/46259-solved-looping-_post-i-feel-helpless-here/#findComment-230361 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.