Jump to content

Putting Array Contents into HTML table


BeanoEFC

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/224006-putting-array-contents-into-html-table/
Share on other sites

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>';
?>

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.