//J'ai d'abord récupéré toutes les données de la base de données
$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;
}
//puis traité en php pour s'afficher comme vous le souhaitez
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>";
Remarque :Il est fortement recommandé d'utiliser mysqli_* au lieu de mysql.