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

From: Date: Tue, 25 Jul 2006 00:28:11 +0000
Subject: Re: Re: SQL Result Set -> HTML Table Fragment (but Simple)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-239747@lists.php.net to get a copy of this message
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@garfieldtech.com 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

« previous php.general (#239747) next »