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: |
Partager