//Först hämtade jag all data från databasen
$result = mysql_query("select * from test2")or die(mysql_error());
$regions = array();
$age_groups = array();
while($det = mysql_fetch_array($result)){
if(!in_array($det['region'], $regions)){
$regions[] = $det['region'];
}
if(!in_array($det['age_group'], $age_groups)){
$age_groups[] = $det['age_group'];
}
$rows[] = $det;
}
//bearbetades sedan i php för att visa som du ville
echo "<table>";
echo "<tr><td>Age group</td>";
foreach($age_groups as $keyAG => $valueAG){
echo "<td colspan='2'>$valueAG</td>";
}
echo "</tr>";
echo "<tr><td>Gender</td>";
foreach($age_groups as $keyAG => $valueAG){
echo "<td>M</td>";
echo "<td>F</td>";
}
echo "</tr>";
foreach($regions as $keyR => $valueR){
echo "<tr>";
echo "<td>".$valueR."</td>";
foreach($age_groups as $keyAG => $valueAG){
foreach($rows as $keyROWS => $valueROWS){
if($valueROWS['region'] == $valueR && $valueROWS['age_group'] == $valueAG){
if($valueROWS['gender'] == 'M'){
echo "<td>".$valueROWS['population']."</td>";
}
elseif($valueROWS['gender'] == 'F'){
echo "<td>".$valueROWS['population']."</td>";
}
}
}
$data[$valueR][$valueAG] = array($maleValue, $femaleValue);
}
echo "</tr>";
}
echo "</table>";
Notera:Det rekommenderas starkt att använda mysqli_* istället för mysql.