Jump to content

Array iteration to tablr creation issue


kiwimediapro

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.