This feels like a stupid question, but it's one that's beyond me at the moment.
I want to build table rows using 3 different arrays, one for each table cell.
Code:
foreach ($array_1 as $this_1)
{
print "<td>Blah1</td> \n";
}
foreach ($array_2 as $this_2)
{
print "<td style=\"text-align:center;\">Blah2</td> \n";
}
foreach ($array_3 as $this_3)
{
print "<td style=\"text-align:center;\">Blah3</td></tr> \n";
}
Of course, what's happening is it's doing 30 cells for $array_1, then 30 cells for $array_2 and then 30 cells for $array_3. I'd like it to do 1 cell from the first array, 1 cell from the second array, one cell from the third array, repeat 30 times.
Help much appreciated, thank you.
Last edited by TygerTyger; 12-17-2006 at 02:14 PM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
while(list($ix, $this_1) = each($array_1) and
list(, $this_2) = each($array_2) and
list(, $this_3) = each($array_3))
{
if($ix == 15)
{
// do something....
}
// rest of while loop....
}
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
That's part of the hidden dangers with the code posted earlier. It just assumes everything is in order, that there are no "gaps" in the arrays, etc...
But... If your three arrays are REALLY, TRULY ALWAYS going to be synchronized, using a FOR loop strategy would be just as good and there would be no hidden traps.
In the previously posted example, if some reason $array_3[5] were not to be set, the entire WHILE would come to a screeching halt. If you prefer using that code, you should consider changing the AND condition to an OR. this would ensure that the loop woud continue to proceed as long as data kept coming in from a least one of the arrays.
Bookmarks