maliary Posted June 26, 2007 Share Posted June 26, 2007 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 https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/ Share on other sites More sharing options...
The Little Guy Posted June 26, 2007 Share Posted June 26, 2007 did you join the two tables? or do 2 seperate queries? Link to comment https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-283185 Share on other sites More sharing options...
maliary Posted June 26, 2007 Author Share Posted June 26, 2007 They are 2 separete queries. Link to comment https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-283188 Share on other sites More sharing options...
maliary Posted June 27, 2007 Author Share Posted June 27, 2007 Any solution for this nested loop? Link to comment https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-283816 Share on other sites More sharing options...
JasonLewis Posted June 27, 2007 Share Posted June 27, 2007 what is the $nval array? you didnt really explain some of your variables etc... Link to comment https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-283827 Share on other sites More sharing options...
maliary Posted June 27, 2007 Author Share Posted June 27, 2007 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 https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-283852 Share on other sites More sharing options...
maliary Posted June 28, 2007 Author Share Posted June 28, 2007 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 https://forums.phpfreaks.com/topic/57292-loop-within-a-loop/#findComment-284799 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.