Jump to content

Build a table based on a given complex multi-dimensional array...


Recommended Posts

Ok I am having allot of trouble building what I need from this array.    Can someone explain how to build a table in attachment 2 from the array in attachment 1. 

 

Example array could be any number of Years and up to 12 months per Year.

example_array.png

 

 

example_table.png

 

I got up to echoing the month, but when I try to asort the given sub-group for the current month, everything goes crazy and I only get 4 sets of numbers with no correlation to the original values.

 


print_r($data);

	$row1 		= '';
	$row2 		= '';
	$row3 		= '';
	$row4		= '';

	$num_col 	= count($data) + 1;
	$num_array 	= count($data);


	for($y = 0; $y <= $num_array-1; $y++) {


		$month = array();

		if (is_array($data))
		{
			$current_year = key($data);

			//echo "<br>Year: " . $current_year;
			$year_contributions = current($data);

			//THIS ASORT SEEMS TO MESS EVERYTHING UP
                               // How ever the print_r statement looks fine, so confused.
			//asort($year_contributions);
		//	print_r($year_contributions);


			$num_of_months		= count($year_contributions)-1;

			$row1 .= "<td colspan=\"{$num_of_months}\">{$current_year}</td>";

			//foreach ($year_contributions AS $month=>)
			//echo (is_array($year_contributions)) ? "true" : "false";
			//$row2 .= "<td>";

			for($m = 0; $m <= $num_of_months-1; $m++) {


				$month_name = key($year_contributions);

				if ($month_name != 'pp')
				{		
					$row2 .= "<td>{$month_name}</td>";
				}		

				next($year_contributions);			
			}

			next($data);
		}


	echo "<table border=\"1\"><tr>";
	echo $row1;
	echo "</tr><tr>";
	echo $row2;
	echo "</tr></table>";

 

okay this is a crappy table I know (I don't really do layout, sorry), but here are the loops for that array and hopefully if the table is not to your liking that should be all you have to play with:

 

echo "<table border = '1'><tr>";
foreach($data as $year => $yearinfo) {
   echo "<td>";
      echo "<table border = '1'>";
         echo "<tr>$year</tr>";
         echo "<tr>";
         foreach($yearinfo as $month => $monthinfo) {
            if ($month != 'pp') {
               echo "<td>";
                  echo "<table border = '1'>";
                        echo "<tr><td>$month</td></tr>";
                     foreach($monthinfo as $val) {
                        echo "<tr><td>$val</td></tr>";
                     } // end foreach $monthinfo	
                  echo "</table>";
               echo "</td>";									
            } // end if $month != 'pp
         } // end foreach $yearinfo
         echo "</tr>";     
      echo "</table>";
   echo "</td>";
} // end foreach $data
echo "</tr></table>"; 

 

p.s.- I'm not really sure what you are trying to sort so I didn't include any kind of sorting in this. I just went off the table layout you made.

Thanks "Crayon Violent" I will try this and get back to you.  I was trying to sort the order values for the months, since they seem to be coming out of order, even though the SQL query that made them has a orderby month in them, I think the merging of the data seems to mess up the order or something.

 

The Contributions table looks like the following.

contrubution_table.png

 

cooldude832 this is a Retirement Fund project, PP stands for Participant Contribution.   CC stands for Company Contribution.   PP is Participant production.   

 

What's your modular CMS system like? 

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.