I am having trouble finding the right way to loop over values when creating a table. I have a table with a list of dates, ids and other data. I need to loop over the table that looks like:
rest_id date galls
133 2011-06-01 75
139 2011-06-12 60
133 2011-06-13 85
139 2011-06-22 25
133 2011-06-30 80
And end up with:
id week 1 week 2 week 3 week 4 week 5
133 75 85 wk3 4 80
139 wk1 60 wk3 25 wk5
I have tried many variations but can't get the loop right. I have been playing with this type of loop for weeks and can't get it.
I have a routine that gets the week of the month:
$weekNum = intval((intval($row_rests['day'])+6)/7);
do { ?>
<td><?php if($weekNum == 1 AND !in_array('1',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '1'); continue;} else echo '1'; array_push($weeklist, '1'); ?></td>
<td><?php if($weekNum == 2 AND !in_array('2',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '2'); continue;} else echo '2'; array_push($weeklist, '2'); ?></td>
<td><?php if($weekNum == 3 AND !in_array('3',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '3'); continue;} else echo '3'; array_push($weeklist, '3'); ?></td>
<td><?php if($weekNum == 4 AND !in_array('4',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '4'); continue;} else echo '4'; array_push($weeklist, '4'); ?></td>
<td><?php if($weekNum == 5 AND !in_array('5',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '5'); continue;} else echo '5'; array_push($weeklist, '5'); ?></td>
<?php
} while ($row_rests = mysql_fetch_assoc($rests));
$weeklist = array();
?>
I have been trying to populate an array as I loop. If the week number is already in the array, than skip that entry. Trouble is, sometimes there will be 5 entries, sometimes 0 or anywhere in between.
I need to get SOME text in each cell but I haven't found the right combo of loops (I should always loop 5 times I think) and array writing. I have tried all combos of continue; and break; and resetting the array in different places.
The above code is within a loop that goes through the rest_ids, and that works fine. It's just this sub-loop that is stumping me.
Ideas?