facets Posted January 1, 2008 Share Posted January 1, 2008 Hey Gang, I have an array I need to display creatively and can not work it out. The data looks something like the following: Barry | Tennis | Captain Barry | Soccer | Goal Kicker Barry | Water Polo | Player I know how to display this using a foreach loop the 'satndard' way but I would like to display it the following way.. Barry | Tennis | Captain | Soccer | Goal Kicker | Water Polo | Player Is there any nice way to do this? tks, Will./ Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/ Share on other sites More sharing options...
thebadbad Posted January 1, 2008 Share Posted January 1, 2008 How is your array looking? Do a print_r. And how exactly will you display it? In a table? Be more specific. Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-427280 Share on other sites More sharing options...
facets Posted January 1, 2008 Author Share Posted January 1, 2008 Thanks for the prompt reply. Here's my array output.. It doesn't match the example data above but this IS the real data. [pre] array(75) { [5106]=> array(6) { [0]=> string(4) "6307" [1]=> string(10) "2007-12-01" [2]=> string(10) "2007-12-31" [3]=> string(3) "yes" [4]=> string(0) "" [5]=> string(6) "252.48" } [5134]=> array(6) { [0]=> string(4) "6269" [1]=> string(10) "2007-12-01" [2]=> string(10) "2007-12-31" [3]=> string(3) "yes" [4]=> string(0) "" [5]=> string(6) "283.03" }[/pre] Displayed in a table. But first column only has new row when there is a new client. Does that make sense.. tia, Will./ Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-427282 Share on other sites More sharing options...
thebadbad Posted January 1, 2008 Share Posted January 1, 2008 Okay, as a bit of exercise I wrote a function to output a table from your two-dimensional array, while blanking out the client when it's a repeat. <?php function array2table($array) { sort($array); echo '<table>'; foreach ($array as $nestedArray) { if ($nestedArray[0] == $prev) { echo ' <tr> <td></td>'; unset($nestedArray[0]); foreach ($nestedArray as $value) { echo '<td>', $value, '</td>'; } echo ' </tr>'; } else { echo ' <tr> '; foreach ($nestedArray as $value) { echo '<td>', $value, '</td>'; } echo ' </tr>'; $prev = $nestedArray[0]; } } echo ' </table>'; } // sample usage, inputting the following two-dimensional array $array = array( array('client1', 'something', 'more'), array('client1', 'another something', 'more'), array('client4', 'Water Polo', 'Player'), array('client2', 'Tennis', 'Captain'), array('client2', 'Soccer', 'Goal Kicker'), array('client63', 'Tennis', 'Captain')); array2table($array); // run the function ?> Outputs: <table> <tr> <td>client1</td><td>another something</td><td>more</td> </tr> <tr> <td></td><td>something</td><td>more</td> </tr> <tr> <td>client2</td><td>Soccer</td><td>Goal Kicker</td> </tr> <tr> <td></td><td>Tennis</td><td>Captain</td> </tr> <tr> <td>client4</td><td>Water Polo</td><td>Player</td> </tr> <tr> <td>client63</td><td>Tennis</td><td>Captain</td> </tr> </table> or forum-formatted: client1 another something more [/td]somethingmore client2SoccerGoal Kicker Captain client4Water PoloPlayer client63TennisCaptain My approach maybe isn't the most elegant, but it works Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-427297 Share on other sites More sharing options...
facets Posted January 2, 2008 Author Share Posted January 2, 2008 Thanks for your reply. That function did the trick! I have another question if someone has time.. I'm using the following code to create my array and display it. I'd like to tally a total for each client but am unsure on how to seperate the client_id and totals for calculation. foreach ($clientID as $clients) { // Get Statement Details $query = "SELECT * FROM statements, invoices WHERE statements.id = invoices.statement_id AND statements.due_date >= '$date1' AND invoices.paid != 'on' AND statements.client_id = '$clients'"; $result = mysql_query($query, $linkID) or die("Data not found : Statement Error."); while($row = mysql_fetch_array($result)) { $contents[] = array($row['client_id'], $row['id'], $row['statement_date'], $row['due_date'], $row['overdue'], $row['amount_owed'], $row['total']); } // End Client Names } foreach ($contents as $nestedArray) { if ($nestedArray[0] == $prev){ $content .= "<tr><td width=120></td>"; unset($nestedArray[0]); foreach ($nestedArray as $value) { $content .= "<td width=120>" . $value . "</td>"; } $content .= "</tr>\n"; } else { $content .= "<tr>"; foreach ($nestedArray as $value) { $content .= "<td width=120>" . $value . "</td>"; } $content .= "</tr>\n"; $prev = $nestedArray[0]; } } Any ideas? tia Will. Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-427964 Share on other sites More sharing options...
facets Posted January 2, 2008 Author Share Posted January 2, 2008 Thanks thebadbad for your help so far.. OK, I have made a small leap forward and have merged all the data I need to display. 2 steps to go. How would I use the php/html code below to only print array position 6 once per client? (In this case 743.24) And highlight alternate client rows. (Some client rows may have one line some may have more) Here's the new array. $contents[] = array($row['client_id'], $row['id'], $row['statement_date'], $row['amount_owed'],$row['total'],$newTotal); Which outputs like so. array(231) { [0]=> array(6) { [0]=> string(4) "5106" [1]=> string(4) "6104" [2]=> string(10) "2007-11-01" [3]=> string(0) "" [4]=> string(6) "263.16" [5]=> string(6) "743.24" } [1]=> array(6) { [0]=> string(4) "5106" [1]=> string(4) "6255" [2]=> string(10) "2007-12-01" [3]=> string(0) "" [4]=> string(5) "59.84" [5]=> string(6) "743.24" } foreach ($contents as $nestedArray) { if ($nestedArray[0] == $prev){ $content .= "<tr><td width=120></td>"; unset($nestedArray[0]); foreach ($nestedArray as $value) { $content .= "<td width=120>" . $value . "</td>"; } $content .= "</tr>\n"; } else { $content .= "<tr>"; foreach ($nestedArray as $value) { $content .= "<td width=120>" . $value . "</td>"; } $content .= "</tr>\n"; $prev = $nestedArray[0]; } } Any pointers would be great.. Will./ Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-428052 Share on other sites More sharing options...
thebadbad Posted January 2, 2008 Share Posted January 2, 2008 No problem, I learn best while coding Your first problem was easy, but I can't seem to get my head around the coloring of alternating client-rows. Instead of unsetting the first var in the nestedArray, when the client is a repeat, I just blanked out the first and sixth var, like this: $nestedArray[0] = $nestedArray[5] = ''; I also put in some double quotes in your HTML. Edited foreach (edited from your last post): foreach ($contents as $nestedArray) { if ($nestedArray[0] == $prev){ $content .= "<tr>"; $nestedArray[0] = $nestedArray[5] = ''; foreach ($nestedArray as $value) { $content .= "<td width=\"120\">" . $value . "</td>"; } $content .= "</tr>\n"; } else { $content .= "<tr>"; foreach ($nestedArray as $value) { $content .= "<td width=\"120\">" . $value . "</td>"; } $content .= "</tr>\n"; $prev = $nestedArray[0]; } } Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-428198 Share on other sites More sharing options...
facets Posted January 5, 2008 Author Share Posted January 5, 2008 I ended up getting the Alternate line highlighting working so I thought I'd post it here. $x = 1; foreach ($contents as $nestedArray) { if ($nestedArray[0] == $prev){ if($x % 2) { $content .= "<tr class=row1>"; } else { $content .= "<tr class=row2>"; } $nestedArray[0] = $nestedArray[4] = ''; foreach ($nestedArray as $value) { $content .= "<td>" . $value . "</td>\n"; } $content .= "</tr>\n"; } else { $x++; if($x % 2) { $content .= "<tr class=row1>"; } else { $content .= "<tr class=row2>"; } foreach ($nestedArray as $value) { $content .= "<td>" . $value . "</td>"; } $content .= "</tr>\n"; $prev = $nestedArray[0]; } } Cheers, Wi|| Quote Link to comment https://forums.phpfreaks.com/topic/83967-solved-formathtml-array-output/#findComment-430835 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.