Re: SQL Result Set -> HTML Table Fragment (but Simple)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Michael B Allen wrote:
Anyone have a PHP fragment that just prints a very simple html table
from the contents of a db result set? I don't want phpmyadmin or anything
remotely that complex. I want something that simply extracts the header
info and prints a basic HTML table. It should be about 10 lines of code
I suspect. If so, can you post it?

Mike


Writing code in a mail window, so forgive any errors or ugly code. This will print a table with the first row as headers of the field names, and each row after that is a database row. I hope.

$firstrow = true;
echo "<table>";
while ( $row = mysql_fetch_assoc($result) ) {
  if ( $firstrow ) {
    echo "<tr>" . implode("</tr><tr>", array_keys($row)) . "</tr>";
    $firstrow = false;
  }

  echo "<tr>";
  foreach ( $row as $value ) {
    echo "<td>$value</td>";
  }
  echo "</tr>";
}
echo "</table>";

Essentially what it does is, loop over each row, and if we're the first row, write out the headers, and for all rows, write out each field. I haven't tested this, just typed it up from memory now, so I'm not sure if it'll compile.

Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux