As the other form suggests, an array is your best bet here. I would say do something like this when you're looping through the SQL result set to build the array:
$query = 'SELECT name, color, rank FROM myTable';
$exec = mysql_query($query);
$data = $names = array();
while($result = mysql_fetch_assoc($exec)) {
$data[$result['rank']][$result['name']] = $result['color'];
$names[] = $result['name'];
}
Then, you'd do something like this to build the first row of names:
echo '<tr>';
foreach($names as $name)
echo '<td>' . $name . '</td>';
echo '</tr>';
And then something like this to build the rest of the rows:
foreach($data as $rank => $array) {
echo '<tr><td>' . $rank . '</td>';
foreach($names as $name)
echo '<td>' . $array[$name] . '</td>';
echo '</tr>';
}
Yeah, the HTML isn't pretty and the code is untested, but in my head it all seems to work! :p