<html>
<head>
    <style type="text/css">
        table,th,td {
            border:1px solid black;
            border-collapse: collapse;
            padding: 5px;
            text-align:center;
        }
    </style>
</head>

<body>

<?php    
    //DATA
    //Lets assume the table has 5 fields, and currently contains 3 rows of data:
    $row[0]['id'] = 1;
    $row[0]['date'] = '2010-07-01';
    $row[0]['tomScore'] = '35';
    $row[0]['dickScore'] = '31';
    $row[0]['harryScore'] = '45';

    $row[1]['id'] = 2;
    $row[1]['date'] = '2010-08-01';
    $row[1]['tomScore'] = '56';
    $row[1]['dickScore'] = '64';
    $row[1]['harryScore'] = '49';

    $row[2]['id'] = 3;
    $row[2]['date'] = '2010-09-01';
    $row[2]['tomScore'] = '44';
    $row[2]['dickScore'] = '65';
    $row[2]['harryScore'] = '43';
    
    
    //Instead of printing a table with columns representing the fields and 
    //each row representing a set of field data (i.e. a record)
    //I want to put the field names down the left side of the table
    //with the records displayed in columns.

    print "<h1>Swapping round rows & columns</h1><p>&nbsp;</p>";
    print "<h2>In the normal order (i.e. rows & columns as per the database).</h2>";
    print "<table><tr><td>id</td><td>date</td><td>tomScore</td><td>dickScore</td><td>harryScore</td></tr>";
    //Iterate through each record in the results set
    //pulling out one record as an associative array (i.e. a set of field/value data) per iteration:
    foreach($row as $resultArray) {
        print "<tr>";
        //Iterate through each field in the current record:
        foreach ( $resultArray as $key=>$val ) {
            print "<td>$val</td>";
            
            //Assign the value for the current field to an array in the fieldHash[currentfield] element.
            //Note the new array element is given an index number 1 higher than the current highest index.
            $fieldHash[$key][] = $val;
        }
        print "<tr>";        
    }
    print "</table>";    
    
    print "<p>&nbsp;</p>";
    print "<h2>Now the 1st column shows the field names & each other <i>column</i> shows a record</h2>";    
    print "<table>";
    //Iterate through each field (i.e. each element in the fieldHash:
    foreach ( $fieldHash as $key=>$valArray ) {
            print "<tr><td>$key</td>";
                foreach ($valArray as $val) {
                    print "<td>$val</td>";
                }
            print "</td>";
    }
    print "<table>";
    
?>
</body>
</html>