BeanoEFC Posted January 10, 2011 Share Posted January 10, 2011 Hi Everyone, I have a problem displaying some information from an array (selected from my database). The array is below. Array ( [0] => Array ( [ssc_skill_categories] => Web [sc_skill_categories] => Programming ) [1] => Array ( [ssc_skill_categories] => Actionscript [sc_skill_categories] => Programming ) [2] => Array ( [ssc_skill_categories] => C# [sc_skill_categories] => Programming ) [3] => Array ( [ssc_skill_categories] => CSS [sc_skill_categories] => Programming ) [4] => Array ( [ssc_skill_categories] => Graphic [sc_skill_categories] => Designers ) [5] => Array ( [ssc_skill_categories] => Logo [sc_skill_categories] => Designers ) [6] => Array ( [ssc_skill_categories] => Illistration [sc_skill_categories] => Designers ) [7] => Array ( [ssc_skill_categories] => Animation [sc_skill_categories] => Designers ) ) What i would like to to is display this information in a table like so: <html> <body> <table> <tr> <td>Programming</td><td>Web</td><td>Actionscript</td><td>C#</td><td>CSS</td> <tr> <tr> <td>Designers</td><td>Graphic</td><td>Logo</td><td>Illistration</td><td>Animation</td> <tr> <table> </body> </html> I have been trying and failing all day to do this. Posting my "progress" will clog up the thread, so for now i wont post it. Does anyone have an idea how i would achieve this? Regards, -Ben Quote Link to comment https://forums.phpfreaks.com/topic/224006-putting-array-contents-into-html-table/ Share on other sites More sharing options...
sasa Posted January 11, 2011 Share Posted January 11, 2011 try <?php $test = Array ( '0' => Array ( 'ssc_skill_categories' => 'Web', 'sc_skill_categories' => 'Programming' ), '1' => Array ( 'ssc_skill_categories' => 'Actionscript', 'sc_skill_categories' => 'Programming' ), '2' => Array ( 'ssc_skill_categories' => 'C#', 'sc_skill_categories' => 'Programming' ), '3' => Array ( 'ssc_skill_categories' => 'CSS', 'sc_skill_categories' => 'Programming' ), '4' => Array ( 'ssc_skill_categories' => 'Graphic', 'sc_skill_categories' => 'Designers' ), '5' => Array ( 'ssc_skill_categories' => 'Logo', 'sc_skill_categories' => 'Designers' ), '6' => Array ( 'ssc_skill_categories' => 'Illistration', 'sc_skill_categories' => 'Designers' ), '7' => Array ( 'ssc_skill_categories' => 'Animation', 'sc_skill_categories' => 'Designers' ) ); $table = array(); foreach ($test as $a) $table = array_merge($table, array_values ($a)); $table = array_unique($table); $table = array_chunk($table, 5); foreach ($table as $k => $r) $table[$k] = '<td>'. implode('</td><td>', $r).'</td>'; echo '<table border="3"><tr>', implode('</tr><tr>', $table),'</tr></table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/224006-putting-array-contents-into-html-table/#findComment-1157747 Share on other sites More sharing options...
BeanoEFC Posted January 11, 2011 Author Share Posted January 11, 2011 Hi sasa, Thanks for the help you are a life saver. I do have a followup questions though. As this data is collected from my database, there will sometimes be null values. The function array_unique() groups all the null values values into a one value. Is there a way to use array_unique() with a clause where if NULL then ignore? Regards, -Ben Quote Link to comment https://forums.phpfreaks.com/topic/224006-putting-array-contents-into-html-table/#findComment-1157904 Share on other sites More sharing options...
BeanoEFC Posted January 11, 2011 Author Share Posted January 11, 2011 Hi again, I did this with the following function function array_unique2($array) { $out = array(); //loop through the inbound foreach ($array as $key=>$value) { //if the item isn't in the array if($value == NULL){ $out[$key] = $value; } if (!in_array($value, $out)) { $out[$key] = $value; } } return $out; } This has seemed to to the trick. Sasa, thank you very much for your help. Its greatly appreciated =) regards, -Ben Quote Link to comment https://forums.phpfreaks.com/topic/224006-putting-array-contents-into-html-table/#findComment-1157921 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.