Jump to content

[SOLVED] Using MYSQL table field names as headers in a HTML table


jonnewton

Recommended Posts

I want to show all the data in a MYSQL table in a series of rows in an HTML table, and can do so with the code listed below.

 

What I would like to do is add an additional row at the top that contains headers for the HTML table using field names retrieved somehow from the MYSQL table itself. I don't know enough about MYSQL code to know how to do this.

 

I know I could hard code the field names, but I want to use this code with different tables which might have different field configuration.

 

Any guidance would be appreciated!

 

Here's the code that shows just the data in a series of rows:

 

$resource=mysql_connect($this->hostname, $this->username, $this->password); 

$my_db = mysql_select_db($this->databasename, $resource);

$query = "SELECT * FROM  $this->table WHERE term = \"$this->term\" AND year = \"$this->year\" ORDER BY last";

$result = mysql_query ($query); ?>

           

<table border="0" cellspacing="1" cellpadding="5"> <?php

 

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

 

echo "<tr class=\"$row_color\">";

 

foreach($row as $data) {

 

$this->draw_cell($data);

 

}

 

echo "</tr>";

 

}

 

echo "</table>";

 

 

OK, that works great. Here's my code:

 

$fields = mysql_num_fields($result);

 

echo "<tr class=\"rowColor1\">";

 

for ($i = 0; $i < $fields; ++$i) {

 

$this->draw_cell(mysql_field_name($result, $i));

 

}

 

echo "</tr>";

 

Thanks again for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.