Jump to content

Php foreach loop with multiple variables


miguelfsf

Recommended Posts

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? 

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)
}

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)
}

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?  

 

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.

 

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  :happy-04:

 

$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 />";
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.