On Monday 24 July 2006 18:50, Adam Zey wrote:

> 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>";

You don't need to pull the headers from the data, actually.  You can access 
the fields in the result object directly.

$result = mysql_query($sql);

echo "<table>";

echo "<tr>";
$num_fields = mysql_num_fields($result);
for($i=0; $i < $num_fields; $i++) {
  echo "<th>", mysql_field_name($result, $i), "</th>";
}
echo "</tr>";

while ( $row = mysql_fetch_row($result) ) {
   echo "<tr><td>" . implode("</td><td>", $row) . "</td></tr>";
}
echo "</table>";

The above should work, I think. :-)

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

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

Reply via email to