kiwimediapro Posted August 10, 2011 Share Posted August 10, 2011 Hi guru's, I have a particular problem i cant seem to get the better of it. I just cant see the solution. I have the code bellow which itterates through an array to create a 3 column table for each year with x no of rows of data and a Totals row to sum each column. The totals have been done via sql and are already in the array dso no need to calculate them just display them as a table footer for each year. Problem is that because that it misses doing the first year totals and attaches the second years to the first and so on. I beleive the issue is with my iteration loop writing out the HTML but cant see it. :-) Below is the code <?php if( !empty($data)){ //$lastmonth=NULL; $lastyear = NULL; foreach($data as $row) { if ($row['year'] != $lastyear) { echo (!$lastyear==NULL ? " <tfoot><tr style='font-weight:bold;font-size:12px'><td>Totals</td><td>".$row['year_totalsales']."</td><td>$".$row['year_amount']."</td></tr></tfoot></table></div>" : NULL); echo " <h3><a href='#'>",$row['year']," - Total Amount ",$row['year_totalsales']," Total Sales $",$row['year_amount'],"</a></h3><div><table width='100%'><tr><th colspan=3></th></tr><tr><th>Month</th><th>Amount</th><th>Sales</th></tr>"; $lastyear = $row['year']; } echo "<tr><td>", $row['month'], "</td><td>", $row['month_amount'], "</td><td>", $row['month_total_sales'], "</td></tr>"; }//--end foreach //$lastyear = NULL; // if ($row['year'] != $lastyear) { //echo (!empty($lastyear)) ? '</table></div>' : NULL; echo " <tfoot><tr style='font-weight:bold;font-size:12px'><td>Totals</td><td>",$row['year_totalsales'],"</td><td>$",$row['year_amount'],"</td></tr></tfoot>"; //} echo "</table></div>"; } ?> Link to comment https://forums.phpfreaks.com/topic/244389-array-iteration-to-tablr-creation-issue/ Share on other sites More sharing options...
Morg. Posted August 10, 2011 Share Posted August 10, 2011 It seems to me that your $lastyear variable is not set before this starts -- could this be ? Link to comment https://forums.phpfreaks.com/topic/244389-array-iteration-to-tablr-creation-issue/#findComment-1255280 Share on other sites More sharing options...
kiwimediapro Posted August 10, 2011 Author Share Posted August 10, 2011 Yes! That is exactly the issue. But I no not how to fix it. :-) Link to comment https://forums.phpfreaks.com/topic/244389-array-iteration-to-tablr-creation-issue/#findComment-1255566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.