Created
September 30, 2012 07:21
-
-
Save dvidsilva/3806150 to your computer and use it in GitHub Desktop.
Auto Create a Table from an associative array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function table($array,$headers = '',$title = ''){ | |
$table = ''; | |
$table .= "<table id=table class=table >\n"; | |
if($title != ''){ | |
$table .= "<caption >$title</caption>\n"; | |
} | |
$table .=" <thead><tr>\n"; | |
$r = 1; | |
if(!is_array($headers)){ | |
$headers = array_keys($array[0]); | |
} | |
foreach ($headers as $head){ | |
$head = str_replace('_',' ',$head); | |
$table.=" <th>".$head."</th>\n"; | |
} | |
$table .= " </tr></thead><tbody>\n"; | |
foreach ($array as $l){ | |
if($r % 2){ $odd= ' odd ';}else{$odd = '';} | |
$table.=" <tr class='$odd'>\n"; | |
foreach($l as $c){ | |
$table .= "<td>$c</td>"; | |
} | |
$table .= " </tr>\n"; | |
$r++; | |
} | |
$table .= "</tbody></table>\n"; | |
return($table); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment