dosser Posted May 19, 2006 Share Posted May 19, 2006 Hi all,I need to construct a loop to deal with an HTML form I have written. Reason is that there is some information that will always need to be entered, but then the number of other entries is not fixed - could be one, but could be lots.<html><form action="process.php" method="post">Recipe: <input type=text name="recipe" size=40><br/><br/>Chef: <input type="text" name="chef1" size=40><br/><br/>Restaurant: <input type="text" name="restaurant1" size=40><br/><br/>Meal: <input type=text name="meal1" size=140><br /><br/>Chef: <input type="text" name="chef2" size=40><br/><br/>Restaurant: <input type="text" name="restaurant2" size=40><br/><br/>Meal: <input type=text name="meal2" size=140><br /><br/><INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!"><input type=reset name"reset" value="reset"></form></html> I.E There must be one entry for recipe (and always only one), chef, restaurant and meal. But there could be more than one instance of the recipe and thus the need for 2nd boxes for chef/restaurant/meal (this will eventually have about 50 repetitions of these). At the moment I have it working but only by writing it all out long hand - and this will not be practical if there are eg 20 repetitions. At moment looks like:<?php$conn=mysql_connect("localhost","******","******");if (!$conn){die('Could not connect to mysql');}$db= mysql_select_db("food",$conn);$recipe_name=mysql_query("insert into recipe values('null','$_POST[recipe]')");$recipe_id=mysql_insert_id();$chef_name=mysql_query("insert into chef values('null','$_POST[chef1]')");$chef_id=mysql_insert_id();$restaurant_name=mysql_query("insert into restaurant values('null','$_POST[restaurant1]','null','null')");$restaurant_id=mysql_insert_id();$meal_info=mysql_query("insert into meal values('null',$recipe_id,$creator_id,'$_POST[meal1]',$restaurant_id)");if ($_POST[chef2]!=null){$chef_name2=mysql_query("insert into chef values('null','$_POST[chef2]')");$chef_id2=mysql_insert_id();}if ($_POST[restaurant2]!=null){$restaurant_name2=mysql_query("insert into restaurant values('null','$_POST[restaurant2]','null','null')");$restaurant_id2=mysql_insert_id();}if ($_POST[meal2]){$meal_info2=mysql_query("insert into meal values('null',$recipe_id,$creator_id2,'$_POST[meal2]',$restaurant_id2)");?>(NB. The database is called food. Four tables are:recipe - recipe_id (primary key, auto-incr), and recipe_namechef - chef_id (pr.key, auto-incr) and chef_namerestaurant - restaurant_id (pr.key, auto-incr) and restaurant_namemeal - meal_id (pr.key, auto-incr), meal_info, recipe_id, creator_id, restaurant_id. )Obviously there must be a better way to do this, I assume using a loop and perhaps an array. What I can not work out is how to link the array in the 'process.php' to the correct box in the html form. What should the syntax be for the HTML form (eg 'chef[]'?), and how do i construct the php code so that it will loop and insert data in the DB if there is any there?Thanks in advanceD Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/ Share on other sites More sharing options...
gijs25 Posted May 19, 2006 Share Posted May 19, 2006 Try this: <input type="text" name="chef[]" size=40> <input type="text" name="chef[]" size=40> <input type="text" name="chef[]" size=40> <input type="text" name="chef[]" size=40> <input type="text" name="chef[]" size=40> <input type="text" name="chef[]" size=40>after posting to a php page echo "<pre>";print_r($_POST);And you will see the posting results as an array. Pretty simple Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37146 Share on other sites More sharing options...
gijs25 Posted May 19, 2006 Share Posted May 19, 2006 and don't double post! Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37153 Share on other sites More sharing options...
dosser Posted May 19, 2006 Author Share Posted May 19, 2006 ok so now what? I can print the list but what I want to be able to do is put them straight into the database with eg $sql=mysql_query("insert into chef values ("....but I don't understand how to construct the loop because the array has both chef[], restaurant [] and meal []? Any ideas?DP.S Sorry for the double post - just read the old one and realised it made little sense! Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37165 Share on other sites More sharing options...
gijs25 Posted May 19, 2006 Share Posted May 19, 2006 [code]$c_meal = count($_POST['meal']);$a_meal = $_POST['meal'];$a_chef= $_POST['chef'];etc etcfor (i=0; $i>=$c_meal; $i++){ $sql="insert into table (meal, chef, recipie) values ($a_meal[$i], $a_chef[$i] etc etc;}[/code]hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37169 Share on other sites More sharing options...
dosser Posted May 19, 2006 Author Share Posted May 19, 2006 Not working - no error messages but no data going into tables. Interestingly if i just do:$total=count($_POST[chef]);echo("$total");I get the result 3, which is correct number of total chef boxes available - however I need to know the number of boxes with any information in.when i try $total=count($_POST[chef]!=null);echo("$total");I get 1 - no matter how many of the boxes are filled in. Thoughts? Thanks for taking the time gijs!D Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37181 Share on other sites More sharing options...
.josh Posted May 19, 2006 Share Posted May 19, 2006 post your code again. Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37187 Share on other sites More sharing options...
dosser Posted May 19, 2006 Author Share Posted May 19, 2006 <? $conn=mysql_connect("localhost","*****","******");if (!$conn){die('Could not connect to mysql');}$db= mysql_select_db("food",$conn);//just to see what would happen - this bit works ok$total=count($_POST[chef]);echo("$total");echo "<pre>";print_r($_POST);//this bit goes into the database fine$recipe_name=mysql_query("insert into recipe values('null','$_POST[recipe]')");$recipe_id=mysql_insert_id();//this is the actual problem$total = count($_POST[chef]);$a_chef = $_POST[chef];$a_restaurant = $_POST[restaurant];$a_meal = $_POST[meal];for ($i=0; $i>=$total; $i++){$chef_name=mysql_query("insert into chef values('null',$a_chef[$i])");$chef_id=mysql_insert_id();$restaurant_name=mysql_query("insert into restaurant values('null',$a_restaurant[$i])");$retaurant_id=mysql_insert_id();$meal_info=mysql_query("insert into meal values('null',$recipe_id,$chef_id,$a_meal[$i],$restaurant_id)");}?>As I say the recipe table works fine, but the other tables have no new entries. No error message appears, only the:echo("$total");echo "<pre>";print_r($_POST);Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/10000-help-with-loops/#findComment-37199 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.