Bonjour,

Je voudrais créer un tableau selon l'image jointe mais j'ai beaucoup de mal à démarrer.
La table lst_countries comprend les colonnes 'code' et 'fr_name'.
La table dat_hotels contient les colonnes:
  • country
  • area
  • city


Nom : tableau.png
Affichages : 624
Taille : 19,7 Ko
Voici mon code. Pour créer les lignes et les cellules du tableau j'utilise des classes largement éprouvées. La fonction getGeogrGroups fonctionne. C'est mon code principal, commenté "Constitution du tableau" qui ne fonctionne pas. Je le trouve moi-même très compliqué et il ne fonctionne pas: Le résultat affiché présente un mélange incohérent de données qui se répètent pour tous les pays.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
function getGeogrGroups(string $groupType, array $where=[]) {
	$db = dbConnect();
 
	if ($groupType === 'country') {
		$sql = "
			SELECT DISTINCT COALESCE(h.country, '') AS code, COALESCE(c.fr_name, '') AS name
			FROM dat_hotels h
				INNER JOIN lst_countries c
				ON h.country=c.id
			ORDER BY c.fr_name
		";
	}
	else {
		$strWhere = empty($where) ? '': 'WHERE ';
		if ($strWhere) {
			foreach ($where as $key=>$clause) {
				$strWhere .= "$key='$clause' AND ";
			}
			$strWhere = substr($strWhere, 0, strlen($strWhere)-5);
		}
		// $groupType est sélectionné deux fois pour l'avoir à la fois comme identifiant et libellé des listes d'options
		$sql = <<<SQL
			SELECT DISTINCT COALESCE($groupType, '') AS code, COALESCE($groupType, '') AS name
			FROM dat_hotels
			%s
			ORDER BY $groupType
SQL;
		$sql = sprintf($sql, $strWhere);
	}
	$sql = str_replace(["\t", PHP_EOL, '  '], ['', ' ', ' '], $sql);
	$stmt = $db->query($sql);
	$result = $stmt->fetchAll();
	return $result;
}
 
// Constitution du tableau
$countries	= getGeogrGroups('country');
if (empty($countries))
	goto endTab;
$th['countries'] = [];
foreach ($countries as $arrCountry) {
	$line	= new tabLine(['class'=>'tr1 center']);
	$line->addCell(new tabCell($arrCountry['name'], ['colspan'=>'1'], true)); // Régler le colspan après avoir traiter les td
	$th['countries'][] = $line;
	$areas = getGeogrGroups('area', ['country'=>$arrCountry['code'] ]);
	if (noData(array_column($areas, 'code')))
		continue;
	foreach ($areas as $area) {
		$line = new tabLine(['class'=>'tr2']);
		$line->addCell(new tabCell($area['name'], ['colspan'=>'1'], true)); // Régler le colspan après avoir traiter les td
		$th['areas'][] = $line;
	}
	$cities = getGeogrGroups('city', ['country'=>$arrCountry['code'], 'area'=>$area['code'] ]);
	if (noData(array_column($cities, 'code')))
		continue;
	foreach ($cities as $city) {
		$line = new tabLine(['class'=>'tr4']);
		$line->addCell(new tabCell($city['name'], ['colspan'=>'1'], true)); // Régler le colspan après avoir traiter les td
		$th['cities'][] = $line;
	}
}
 
// Création de la variable d'affichage pour le template
foreach ($th['countries'] as $country) {
	$echo['table'] .= $country;
	foreach ($th['areas'] as $area) {
		if ($area)
			$echo['table'] .= $area;
		foreach ($th['cities'] as $city) {
			if ($cities)
				$echo['table'] .= $city;
		}
	}
}
 
endTab: