foreach loop problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

foreach loop problem

Post by matthew0786 »

I am having trouble working with a foreach loop

I enter this in the textboxes (which are dynamically added text boxes). Say i generate 2 textboxes and i enter the following data.
Data
e1 (<input type="text" name="myInputs[]"> Entry1)
a1 (<input type="text" name="myInputs2[]"> Amount1)

e2 (<input type="text" name="myInputs[]">Entry2)
a2 (<input type="text" name="myInputs2[]">Amount2)


then i post to the next page which contains the following code.

Code: Select all

 
$myInputs = $_POST["myInputs"];
$myInputs2 = $_POST["myInputs2"];
 
$myInputC = 1;
$myInputC2 = 1;
 
 foreach ($myInputs as $eachInput) {
         echo "Entry  " . $myInputC  . " - " . $eachInput . "<br>";
     
                      foreach ($myInputs2 as $eachInput) {
                         echo "Amount  " . $myInputC2 . " - " . $eachInput . "<br>";
                }
 $myInputC++;
  $myInputC2++;
}
 
the 2nd foreach loop nests in the 1st foreach loop and it outputs like this

Entry 1 - e1
Amount 1 - a1
Amount 2 - a2
Entry 3 - e2
Amount 3 - a1
Amount 4 - a2

when im trying to get to show something like this
Entry 1 - e1 Amount 1 - a1
Entry 2 - e2 Amount 1 - a2

I can get it to work if i put the foreach loop separately, but as i'm going to place this into a database, i want it to be able to INSERT into the table in one go.

Code: Select all

 
 mysql_query("INSERT INTO `$db_name`.`$tbl_name` (`Entry1`, `Amount1`) VALUES ('$eachInput', '$eachInput2')");
 
something like that.
sujithtomy
Forum Commoner
Posts: 46
Joined: Tue Mar 24, 2009 4:43 am

Re: foreach loop problem

Post by sujithtomy »

Hello,

You have added <br> in the first loop which causes to split.

try this code instead ...

Code: Select all

 
$myInputs = $_POST["myInputs"];
$myInputs2 = $_POST["myInputs2"];
 
$myInputC = 1;
$myInputC2 = 1;
 
 foreach ($myInputs as $eachInput) {
         echo "Entry  " . $myInputC  . " - " . $eachInput . " &nbsp; ";
     
                      foreach ($myInputs2 as $eachInput) {
                         echo "Amount  " . $myInputC2 . " - " . $eachInput . "<br>";
                }
 $myInputC++;
  $myInputC2++;
}
 
Good Luck :idea:
matthew0786
Forum Newbie
Posts: 9
Joined: Sat Feb 28, 2009 8:01 am

Re: foreach loop problem

Post by matthew0786 »

ok, the <br> help but it still came up with duplicates when display. then i figured out below and it works now

Code: Select all

 
foreach ($myInputs as $keyInput => $valueInput) {
          echo "Entry  " . $myInputCount  . " - " . $valueInput . " &nbsp; ";
         
          echo "Amount  " . $myInputCount . " - " . $myInputs2[$keyInput] . "<br>";
   
    $myInputCount++;        
 
 
 }
 
Post Reply