Skip to content

Instantly share code, notes, and snippets.

@nomanson
Created August 4, 2012 20:32
Show Gist options
  • Save nomanson/3259771 to your computer and use it in GitHub Desktop.
Save nomanson/3259771 to your computer and use it in GitHub Desktop.
PHP make html table function
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