edoggie Posted August 2, 2008 Share Posted August 2, 2008 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. 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/117784-build-a-table-based-on-a-given-complex-multi-dimensional-array/ Share on other sites More sharing options...
.josh Posted August 2, 2008 Share Posted August 2, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/117784-build-a-table-based-on-a-given-complex-multi-dimensional-array/#findComment-605840 Share on other sites More sharing options...
cooldude832 Posted August 2, 2008 Share Posted August 2, 2008 can we see the original query? Also can u define PC/CC/PP to use a bit better Quote Link to comment https://forums.phpfreaks.com/topic/117784-build-a-table-based-on-a-given-complex-multi-dimensional-array/#findComment-605881 Share on other sites More sharing options...
edoggie Posted August 4, 2008 Author Share Posted August 4, 2008 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. 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? Quote Link to comment https://forums.phpfreaks.com/topic/117784-build-a-table-based-on-a-given-complex-multi-dimensional-array/#findComment-607662 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.