Skip to content

Instantly share code, notes, and snippets.

@nomanson
Created August 4, 2012 20:32
Show Gist options
  • Select an option

  • Save nomanson/3259771 to your computer and use it in GitHub Desktop.

Select an option

Save nomanson/3259771 to your computer and use it in GitHub Desktop.

Revisions

  1. nomanson created this gist Aug 4, 2012.
    45 changes: 45 additions & 0 deletions makeHTMLTable.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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>";

    }

    }