Page 1 of 1

db fields as vertical table headers

Posted: Sat Feb 27, 2010 11:05 pm
by nay4
Good day to all! This is my first post and I just started learning PHP a few weeks ago..

So I need help regarding how I can create a table where the fields from my database table are displayed vertically..

This is what I came up with:

Code: Select all

 
<?php 
            $result = mysql_query("SELECT * FROM tblcustomer WHERE CustID = 1");
            if (!$result) {
                die("Query to show fields from table failed");
            }           
            $fields_num = mysql_num_fields($result);
                        
            echo "<table border='1'>";          
            for($i=0; $i<$fields_num; $i++)
            {
                $field = mysql_fetch_field($result);
                echo "<tr><td><strong>{$field->name}</strong>". /*while loop must be here*/ "</td></tr>"; 
                            
            }
            
            while($row = mysql_fetch_row($result)) {
                    foreach($row as $teedee)
                    echo "<tr><td>$teedee</td></tr>";
                }           
            echo "</table>";
?>
I was thinking inserting the while loop inside my comment in the for loop. Is that possible? Or are there any other solutions?