Skip to content

Instantly share code, notes, and snippets.

@gabssnake
Created March 27, 2013 15:18
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 gabssnake/5255018 to your computer and use it in GitHub Desktop.
Save gabssnake/5255018 to your computer and use it in GitHub Desktop.
database to html table
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Table results</title>
<style>
body { margin: 20px; font: 1em/1.5 Geneva, sans-serif; }
table, td { border: 1px solid #bbb; border-collapse: collapse; }
td { padding: 5px 10px; } th { padding: 15px 10px; }
</style>
</head>
<body>
<?php
function tableHtml($sql) {
$thead = $trow = $tbody = '';
if($sql){
while ($line = mysql_fetch_assoc($sql)) {
foreach ($line as $label => $value) {
if($i < count($line)) { $thead .= "\t\t<th>".$label."</th>\n"; $i++; }
$trow .= "\t\t<td>".$value."</td>\n";
}
$tbody .= "\t<tr>\n".$trow."\t</tr>\n"; $trow = '';
} return "<table>\n\t<tr>\n".$thead."\t</tr>\n".$tbody."</table>";
} else { return "<table>\n\t<tr>\n\t\t<td>No entries were found.</td>\n\t</tr>\n</table>"; }
}
mysql_connect('localhost', 'root', 'root');
mysql_select_db('tagscript01');
$sql = "";
echo tableHtml($sql);
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment