//Nejprve jsem načetl všechna data z databáze
$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;
}
//poté zpracováno v php, aby se zobrazilo, jak jste chtěli
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>";
Poznámka:Důrazně se doporučuje používat místo mysql mysqli_*.