miguelfsf Posted June 26, 2013 Share Posted June 26, 2013 (edited) Hello, This is my form: <form name="form" action="..."><div style="border:1px solid grey;"> <b>Song Name'.$i.':</b> <input type="text" name="song[]"> Link 1: <input type="text" name="link1[]"><br> Link2: <input type="text" name="link2[]"><br> Link3: <input type="text" name="link3[]"><br> </div> </form> This is my code: foreach($_POST['song'] as $item1){ //INSERT CODE for songname,link1,link2 and link3 } My for each loop is only optimized for inserting the song name. I want to get the value of the song,link1,2,3 and insert them at the same time. How can i do it? Edited June 26, 2013 by miguelfsf Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/ Share on other sites More sharing options...
dalecosp Posted June 26, 2013 Share Posted June 26, 2013 Add a counter? Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437971 Share on other sites More sharing options...
miguelfsf Posted June 26, 2013 Author Share Posted June 26, 2013 How? Could you be more specific? Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437972 Share on other sites More sharing options...
AbraCadaver Posted June 26, 2013 Share Posted June 26, 2013 (edited) Its hard to tell without your other code. I assume you're using song[] array because you have multiple songs in the form? Also, I assume you have $i incrementing? '<form name="form" action="..."><div style="border:1px solid grey;"> <b>Song Name'.$i.':</b> <input type="text" name="song['.$i.']"> Link1: <input type="text" name="link['.$i.'][]"><br> Link2: <input type="text" name="link['.$i.'][]"><br> Link3: <input type="text" name="link['.$i.'][]"><br> </div> </form>'; Make sure to use prepared statements or sanitize the data before insert: foreach($_POST['song'] as $i => $song) { $data = "'$song','" . implode("','", $_POST['link'][$i]) . "'"; //INSERT INTO table_name VALUES ($data) } Edited June 26, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437983 Share on other sites More sharing options...
miguelfsf Posted June 26, 2013 Author Share Posted June 26, 2013 (edited) First of all thanks for the help, I appreaciate it. That would work if in my db table for songs I had only one field for link but I have 3 fields(link1,link2 and link3). I have multiple songs in the form and for each song there are 3 different links. Could you understand what im saying? Sorry my bad english.. Can you explain also what implode is meant to do? Thanks. foreach($_POST['song'] as $i => $song) { $data = "'$song','" . implode("','", $_POST['link'][$i]) . "'"; //INSERT INTO table_name VALUES ($data) } Edited June 26, 2013 by miguelfsf Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437986 Share on other sites More sharing options...
Solution dalecosp Posted June 26, 2013 Solution Share Posted June 26, 2013 When arrays are created without assigned indexes, they are indexed by numbers, starting with zero.As all your arrays appear to meet this criterion, you should be able to do something like this: $ct = 0; // initialize a counter foreach($_POST['song'] as $item1){ echo "Song: $item1 <br />Link 1 ".$_POST['link1'][$ct]."<br />Link 2 ".$_POST['link2'][$ct]."<br />Link 3 ".$_POST['link3'][$ct]."<br />"; $ct++; }Obviously, you're probably not planning to just print out the data, but hopefully you get the idea? Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437987 Share on other sites More sharing options...
AbraCadaver Posted June 26, 2013 Share Posted June 26, 2013 First of all thanks for the help, I appreaciate it. That would work if in my db table for songs I had only one field for link but I have 3 fields(link1,link2 and link3). I have multiple songs in the form and for each song there are 3 different links. Could you understand what im saying? Sorry my bad english.. Can you explain also what implode is meant to do? Thanks. The implode is doing exactly what you are asking about. $data will be 'song','link1','link2','link3'. Why don't you try it and echo your query to see what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437992 Share on other sites More sharing options...
miguelfsf Posted June 26, 2013 Author Share Posted June 26, 2013 When arrays are created without assigned indexes, they are indexed by numbers, starting with zero. As all your arrays appear to meet this criterion, you should be able to do something like this: $ct = 0; // initialize a counter foreach($_POST['song'] as $item1){ echo "Song: $item1 <br />Link 1 ".$_POST['link1'][$ct]."<br />Link 2 ".$_POST['link2'][$ct]."<br />Link 3 ".$_POST['link3'][$ct]."<br />"; $ct++; }Obviously, you're probably not planning to just print out the data, but hopefully you get the idea? This worked! Thank you both for helping me Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437993 Share on other sites More sharing options...
kicken Posted June 26, 2013 Share Posted June 26, 2013 $ct = 0; // initialize a counter foreach($_POST['song'] as $item1){ echo "Song: $item1 <br />Link 1 ".$_POST['link1'][$ct]."<br />Link 2 ".$_POST['link2'][$ct]."<br />Link 3 ".$_POST['link3'][$ct]."<br />"; $ct++; } On a small note, the counter as you have it is not really necessary, you can use the index of the song array item to access the other arrays: foreach ($_POST['song'] as $idx=>$item1){ echo "Song: $item1 <br />Link 1 ".$_POST['link1'][$idx]."<br />Link 2 ".$_POST['link2'][$idx]."<br />Link 3 ".$_POST['link3'][$idx]."<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437995 Share on other sites More sharing options...
miguelfsf Posted June 26, 2013 Author Share Posted June 26, 2013 Looks better, im going to try it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/279596-php-foreach-loop-with-multiple-variables/#findComment-1437997 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.