Jump to content

help with loops


dosser

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!
Link to comment
Share on other sites

[code]
$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;
}
[/code]


hope this helps
Link to comment
Share on other sites

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
Link to comment
Share on other sites

<? $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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.