Skip to content

Instantly share code, notes, and snippets.

@dvidsilva
Created September 30, 2012 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvidsilva/3806150 to your computer and use it in GitHub Desktop.
Save dvidsilva/3806150 to your computer and use it in GitHub Desktop.
Auto Create a Table from an associative array
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