Forum Moderators: coopster

Message Too Old, No Replies

skip column where there is no data

         

bouton

12:24 pm on Dec 17, 2007 (gmt 0)

10+ Year Member



I am trying to do something which I thought would be very simple. I want this table


col1 col2 col3 col4 col5 ... col12
1 2 3 4 5 12
1 3 4 12
1 2

That is, I want to print "1" in the first column, "2" in the second column etc, indicating the months as extracted from a mysql database, with "blanks" where there are no records in that month

data array looks like
(1,2,,3,4,,5,12)
(1,3,4,,12)
(1,2)

The info is coming from an sql query which selects distinct month(date) where year ="2007".

I have tried all sorts of loops, for $i's, counting.. and I can't get the thing to loop, missing out where there is no number for that column.

This prints them out fine in columns, but when a month is missing, it doesn't skip a column...


<table width="100%" border="0">
<tr>
<?php
$query = GetModelsMonths($year);
if ($modelMonths = $db->get_results($query)) {
foreach ($modelMonths as $modelMonth) {?>
<td><?php echo $modelMonth->month;?></td>
<?php
} // end foreach
} // end if
?>
</tr>
</table>

Thanks

cameraman

5:46 pm on Dec 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:
if ($modelMonths = $db->get_results($query)) {
$line = array_fill(1,12,'<td>&nbsp;</td>');
foreach ($modelMonths as $modelMonth)
if($modelMonth->month)
$line[$modelMonth->month] = '<td>' . $modelMonth->month . '</td>';
$line = implode("\n",$line);
echo $line;
}

bouton

5:56 pm on Dec 17, 2007 (gmt 0)

10+ Year Member



I just finished and thought I'd write back with may answer - and voila, a similar answer is posted. I did indeed use if in_array - and it seems to work

Thanks - will try your method as well.

K
How I did it


<tr>
<?php
// use mysql group_concat to get back an array of months
$query = GetMonthsConcat($model,$modelYear->year);
if ($modelMonths = $db->get_results($query)) {
foreach ($modelMonths as $months) {
$month_array = explode(",",$months->months);
for ($y=1;$y<=12;$y++) {
if(in_array($y,$month_array)) {?>
echo "<td>".$y;?>."</td>";0
} else {
echo "<td class='missing'>&nbsp;</td>";
}
} // end for y
} // end foreach
} // end if?>
</tr>

Thanks