manuelV Posted August 16, 2011 Share Posted August 16, 2011 Basically I have this code <?php $myInput = $_POST["myInput"]; // these have more than one value to it $myInput2 = $_POST["myInput2"]; foreach ($myInput as $eachInput ) { echo $eachInput ."<br>"; } foreach ($myInput2 as $eachInput2 ) { echo $eachInput2 ."<br>"; } ?> This code will input however many values are gathered from the $_POST. Works just fine. But what I need is for there to be one foreach loop, as supposed to two. I need both of those to be in a single foreach. Is there a way of doing this? Reason for this is the values will be inserted into a SQL table, in two different columns. So I need to be able to do it with a single query! Any help is appreciated Link to comment https://forums.phpfreaks.com/topic/244961-need-help-with-foreach-loop/ Share on other sites More sharing options...
msaz87 Posted August 16, 2011 Share Posted August 16, 2011 Well assuming they line up (e.g. the first value in myInput goes with the first value in myInput2) you could do something like: $count = 0; foreach ($myInput as $eachInput ) { echo $eachInput ."<br>"; echo $myInput2[$count]; $count++; } Link to comment https://forums.phpfreaks.com/topic/244961-need-help-with-foreach-loop/#findComment-1258316 Share on other sites More sharing options...
phpSensei Posted August 16, 2011 Share Posted August 16, 2011 Does input2 also have multiple fields? Link to comment https://forums.phpfreaks.com/topic/244961-need-help-with-foreach-loop/#findComment-1258321 Share on other sites More sharing options...
manuelV Posted August 17, 2011 Author Share Posted August 17, 2011 Does input2 also have multiple fields? Yes it does. I managed to get it to work like this <?php $myInputs = $_POST["myInputs"]; $myInputs2 = $_POST["myInputs2"]; foreach($myInputs as $k=>$eachInput) { $eachInput2 = $myInputs2[$k]; mysql_query("INSERT INTO contactInfo (contactValue, contactType) VALUES ('$eachInput2','$eachInput')"); } ?> Link to comment https://forums.phpfreaks.com/topic/244961-need-help-with-foreach-loop/#findComment-1258332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.