Jump to content

loop within a loop


maliary

Recommended Posts

Hi,

 

I have the following loop within a loop problem.

 

The while loop, obtains data from the database from table x. The for loop obtains similar data from the database from table y.

This

 $tp['nr'] 

  comes from table x, while

 $nval[$j] 

comes from table y.

 

The if statement

 if ($parameterselect == $pio) 

checks if $parameter select - a variable is equal to $pio a value from the database if they are the following if statements follow.

 

If they are identical or the same this

$rval[$j]

is printed out from table y if they are not then

 $tp[test_value] 

from table x is printed out.

 

The problem is it prints out multiple outputs. Which messes up the display. This is obvious in a loop within a loop but is there a solultion to this?

 

 

while($tp=$tparams->FetchRow()){

              

        if ($parameterselect == $pio) {
               
        for ($j = 0; $j < $numname; $j++){  
             
            
          if  ($nval[$j] == $tp['nr'] )
          {
   echo "<td><input name=test_value".$counter." type=text size=8 value= $rval[$j]></td>"; 
  
          } else
          {
               
          

   echo "<td class=a10_b><input name=test_value".$counterup." type=text size=8 value= $tp[test_value]></td>"; 
   
          
          }     
        
        } }  else
        {
     

    # second column        
   echo "<td><input name=test_value".$counter." type=text size=8 value=$tp[test_value]></td>";      
   
                     
    $counter++; 

        }
}
}

 

 

In summary is there a way of preventing the multiple out puts in loops within loops?

 

Link to comment
Share on other sites

 

 

Okay,

 

let me explain the variables

 

$parameterselect can hold a value e.g. mango - it comes from a select box - but we can use mango for now.

 

$pio holds values from a table e.g. mango or orange etc. - for now we can place it as mango

 

 

the $nval[$j] array holds an array of numbers from table y  e.g. $nval = array('1','2','3','4');

the $tp['nr']  array holds an array of numbers from table x e.g.  $tp  = array('1','2','3','7',)

 

the  $rval[$j] array holds values from table x, $tp[test_value] holds values from table y.

 

if both $parameterselect and $pio are both mango then the for loop will execute. and where the elements of the array $nval and $tp match then an element of $rval gets displayed if not an element of $tp[test_value] will get displayed.

 

This works but multiple outputs are made. Creating a messy display.

 

Link to comment
Share on other sites

 

Guys,

 

I found a solution.

 

The reason for the second loop was to be able to retrive the values in the nval and rval array. But a loop isn't neccesary for this.

 

The counter works well for this.  So....

$nval[$counter]

and

$rval[$counter] 

does the trick.

 

while($tp=$tparams->FetchRow()){

              

        if ($parameterselect == $pio) {
               
        
             
            
          if  ($nval[$counter] == $tp['nr'] )
          {
   echo "<td><input name=test_value".$counter." type=text size=8 value= $rval[$counter]></td>"; 
  
          } else
          {
               
          

   echo "<td class=a10_b><input name=test_value".$counter." type=text size=8 value= $tp[test_value]></td>"; 
   
          
             
        
        } }  else
        {
     

    # second column        
   echo "<td><input name=test_value".$counter." type=text size=8 value=$tp[test_value]></td>";      
   
                     
    $counter++; 

        }
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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