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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.