Created
August 4, 2012 20:32
-
-
Save nomanson/3259771 to your computer and use it in GitHub Desktop.
PHP make html table function
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
function make_table($sql) | |
{ | |
$res = mysql_query("$sql"); | |
if (!$res) | |
{ | |
die("Query to show fields from table failed"); | |
} | |
$rows_num = mysql_num_rows($res); | |
if (!$rows_num) | |
{ | |
echo "<p>No information avaliable!</p>"; | |
} | |
else | |
{ | |
$fields_num = mysql_num_fields($res); | |
echo "<table><tr>"; | |
// printing table headers | |
for ($i = 0; $i < $fields_num; $i++) | |
{ | |
$field = mysql_fetch_field($res); | |
echo "<th>{$field->name}</td>"; | |
} | |
echo "</tr>\n"; | |
// printing table rows | |
while ($row = mysql_fetch_row($res)) | |
{ | |
echo "<tr>"; | |
// $row is array... foreach( .. ) puts every element | |
// of $row to $cell variable | |
foreach ($row as $cell) echo "<td>$cell</td>"; | |
echo "</tr>\n"; | |
} | |
echo "</table>"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment