Andrew R Posted March 18, 2009 Share Posted March 18, 2009 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! Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/ Share on other sites More sharing options...
pkSML Posted March 18, 2009 Share Posted March 18, 2009 There's no need to perform a foreach loop on $_POST data. There will always only be one value for each posted field. Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787852 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 What exactly are you trying to do? There may be a better solution. Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787853 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 There's no need to perform a foreach loop on $_POST data. There will always only be one value for each posted field. That's not true, you can have an array of posted values. Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787859 Share on other sites More sharing options...
Zane Posted March 18, 2009 Share Posted March 18, 2009 use extract to do what you are trying to do extract($_POST); Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787860 Share on other sites More sharing options...
Andrew R Posted March 18, 2009 Author Share Posted March 18, 2009 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 More sharing options...
pkSML Posted March 18, 2009 Share Posted March 18, 2009 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 More sharing options...
pkSML Posted March 18, 2009 Share Posted March 18, 2009 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 More sharing options...
Andrew R Posted March 18, 2009 Author Share Posted March 18, 2009 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 More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 Use this: $n = array_combine($_POST['description'], $_POST['price']); foreach($n AS $desc => $price) { echo "description: " . $desc . " - Price: " . $price . " "; } Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787892 Share on other sites More sharing options...
pkSML Posted March 18, 2009 Share Posted March 18, 2009 <?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 More sharing options...
anf.etienne Posted March 18, 2009 Share Posted March 18, 2009 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 More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 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 More sharing options...
anf.etienne Posted March 18, 2009 Share Posted March 18, 2009 is this only limited to pairs? Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787947 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 is this only limited to pairs? Stop hi-jacking threads. If you have a question please create your own, I didn't realize you weren't the OP. To answer your question, Read more here. Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787950 Share on other sites More sharing options...
anf.etienne Posted March 18, 2009 Share Posted March 18, 2009 i didn't know asking a question about a code you put was hijacking a thread as i haven't posted anything but questions on what you posted????????????? :-\ Link to comment https://forums.phpfreaks.com/topic/150019-foreach-help/#findComment-787974 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.