Jump to content

Recommended Posts

For example I have the following form array

 

<?php 

$how_many_fields = $_POST['how_many_fields']; 

for ($i = 1; $i <= $how_many_fields; $i++) { ?>
<input name="description[]" type="text" id="description[]">
<input name="price[]" type="text" id="price[]" />
<?php } ?>

 

and I am trying to insert the data into a table. 

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787862
Share on other sites

For example I have the following form array

 

<?php 

$how_many_fields = $_POST['how_many_fields']; 

for ($i = 1; $i <= $how_many_fields; $i++) { ?>
<input name="description[]" type="text" id="description[]">
<input name="price[]" type="text" id="price[]" />
<?php } ?>

 

and I am trying to insert the data into a table.

 

Here's how I would do it:

<?php 

$how_many_fields = $_POST['how_many_fields']; 

for ($i = 1; $i <= $how_many_fields; $i++) {
echo "<input name=\"description[$i]\" type=\"text\" id=\"description[$i]\">".
"<input name="price[$i]" type="text" id="price[$i]" />"
} 

echo "<input type='hidden' value='{$how_many_fields}' name='fieldnums'";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787873
Share on other sites

Hi there.

 

Why does the following code not work or if it isn't possible how would I do something to the same effect?

 

<?php foreach($_POST["description"] as $description && $_POST["price"] as $price)    {    


///do stuff here
}
?>

 

Thanks a million!

As for this code, try this:

<?php for($i = 1; $i <= $_POST['fieldnums']; $i++)    {    


///do stuff here
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787876
Share on other sites

Thanks a million for that

 

One thing though, How would I get the corresponding rows to match up?

 

I tried the following....

 

<?php

for($i = 1; $i <= $_POST['fieldnums']; $i++)    {  

$description = $_POST['description']; 
$price= $_POST[price]; 

mysql_query("INSERT INTO table_name_here_break_down (description,price) VALUES ('$description[$i]','$price[$i]')") or die (mysql_error());


} 

?>

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787889
Share on other sites

<?php

for($i = 1; $i <= $_POST['fieldnums']; $i++)    {  

$description = $_POST['description']; 
$price= $_POST[price]; 

mysql_query("INSERT INTO table_name_here_break_down (description,price) VALUES ('$description[$i]','$price[$i]')") or die (mysql_error());
} 
?>

 

Try this: (if needed)

<?php
for($i = 1; $i <= $_POST['fieldnums']; $i++)    {  

$description = $_POST['description'][$i]; 
$price= $_POST['price'][$i]; 

mysql_query("INSERT INTO table_name_here_break_down (description,price) VALUES ('$description','$price')") or die (mysql_error());
} 
?>

 

I edited this to make it work right. (I also hope you caught the few times I forgot to escape a double-quote a few posts ago. I also forgot to add a semicolon.)

 

Maq's example would work as well. Just insert your query in his example with the variables $desc and $price.

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787900
Share on other sites

Use this:

 

$n = array_combine($_POST['description'], $_POST['price']);
foreach($n AS $desc => $price) 
{
   echo "description: " . $desc . " - Price: " . $price . "<br/>";
} 

 

hi maq....if i need more than one variable adding into a foreach loop is array_combine a good way to do? is it possible to use the && operator within a foreach()?

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787931
Share on other sites

array_combine() creates an associative array.  So now you have your description and prices linked.

 

For testing to make sure they're linked properly, this will give you an idea of how this function works:

 

$n = array_combine($_POST['description'], $_POST['price']);
print_r($n);

 

For insertion use something like:

 

$n = array_combine($_POST['description'], $_POST['price']);
foreach($n AS $desc => $price)
{
   $sql = "INSERT INTO table_name_here_break_down (description, price) VALUES ('$desc', '$price')"
   mysql_query($sql) or die (mysql_error());
   echo "Query: " . $sql . "
";
}

Link to comment
https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787933
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.