View Full Version : Putting data into a table

03-04-2007, 10:04 PM

I am displaying data from a database.
I want it to look neater on the html page.
Is there a way of inserting the data from the database into a dynamic table.
How would i do that?

Thank you

03-04-2007, 10:17 PM
Here is a generic function that accepts a resource result variable and outputs a table -

function Wrap($result) {

// Printing results in HTML
echo "<table border=1>\n";
echo "\t<tr>\n";
/* get column metadata */
$i = 0;
while ($i < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $i);
echo "\t\t<td><b>".$meta->name."</b></td>\n";
echo "\t</tr>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
echo "\t</tr>\n";
echo "</table>\n";
} // end of wrap functionTypical usage -

$query = 'SELECT * FROM your_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

03-04-2007, 10:37 PM
thanks thats great

03-15-2007, 04:20 AM

Thanks CFMaBiSmAd...its great!! One thing though...at the moment the attribute name sets the size of the column which means any value under this attribute longer than the attribute name will result in the value displayed on two lines. I was wondering if it is possible to prevent this so all values will be set one line? For example maybe letting the values set the column size rather than the attribute. Ive been trying to modify it but to no avail. Let me know if im not making sense!

Any ideas? Thanks for all your help :thumbsup: