Jump to content


Photo

help with loops


  • Please log in to reply
7 replies to this topic

#1 dosser

dosser
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 May 2006 - 01:26 PM

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_name
chef - chef_id (pr.key, auto-incr) and chef_name
restaurant - restaurant_id (pr.key, auto-incr) and restaurant_name
meal - 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 advance

D


#2 gijs25

gijs25
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationNetherlands

Posted 19 May 2006 - 01:32 PM

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


#3 gijs25

gijs25
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationNetherlands

Posted 19 May 2006 - 01:42 PM

and don't double post!

#4 dosser

dosser
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 May 2006 - 02:22 PM

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?

D

P.S Sorry for the double post - just read the old one and realised it made little sense!

#5 gijs25

gijs25
  • Members
  • Pip
  • Newbie
  • 9 posts
  • LocationNetherlands

Posted 19 May 2006 - 02:41 PM

$c_meal = count($_POST['meal']);
$a_meal = $_POST['meal'];
$a_chef= $_POST['chef'];
etc etc

for (i=0; $i>=$c_meal; $i++){
 $sql="insert into table (meal, chef, recipie) values ($a_meal[$i], $a_chef[$i] etc etc;
}


hope this helps

#6 dosser

dosser
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 May 2006 - 03:25 PM

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

#7 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 19 May 2006 - 03:40 PM

post your code again.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#8 dosser

dosser
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 19 May 2006 - 04:25 PM

<? $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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users