Created
March 27, 2013 15:18
-
-
Save gabssnake/5255018 to your computer and use it in GitHub Desktop.
database to html table
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
<!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