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

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

 



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
larry@xxxxxxxxxxxxxxxx		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


[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