Bonjour à tous,

Je reprends un ancien code qui fonctionne et qui réalise un arbre généalogique inversé (voir image).
Nom : Capture d’écran 2024-12-14 150046.png
Affichages : 148
Taille : 10,4 Ko

Schéma table base de données:
Code sql : 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
CREATE TABLE IF NOT EXISTS `dat_persons` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_abo` int(10) UNSIGNED NOT NULL,
  `gender` char(1) NOT NULL,
  `lastname` varchar(30) NOT NULL,
  `firstname` varchar(30) NOT NULL,
  `middlename` varchar(45) DEFAULT NULL COMMENT 'autres prénoms',
  `birthyear` char(4) DEFAULT NULL,
  `birthmonth` char(2) DEFAULT NULL,
  `birthday` char(2) DEFAULT NULL,
  `birthplace` varchar(40) DEFAULT NULL,
  `christeningyear` char(4) DEFAULT NULL,
  `christeningmonth` char(2) DEFAULT NULL,
  `christeningday` char(2) DEFAULT NULL,
  `deathyear` char(4) DEFAULT NULL,
  `deathmonth` char(2) DEFAULT NULL,
  `deathday` char(2) DEFAULT NULL,
  `deathplace` varchar(40) DEFAULT NULL,
  `profession` varchar(60) DEFAULT NULL,
  `comment` tinytext COMMENT 'annotations',
  `id_father` int(10) UNSIGNED DEFAULT NULL COMMENT 'clefPere',
  `id_mother` int(10) UNSIGNED DEFAULT NULL COMMENT 'clefMere',
  `create_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `update_date` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `person` (`id_abo`,`lastname`,`firstname`,`birth_date`,`id_mother`),
  KEY `father_id` (`id_father`),
  KEY `mother_id` (`id_mother`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Code php : 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
session_start();
 
require_once "../../config.php";
require_once "../../define.php";
require_once DIR_ROOT."model/model.php";
require_once DIR_ROOT."view/lang/{$_SESSION['language']}.php";
 
//.............................................
// this function is in '/model/model.php'
function getChildren(int $parentId): array|false {
	$db = dbConnect();	// Contient les paramètres PDO et retourne la base de données
 
	$sql = <<<SQL
	SELECT id, gender, firstname, lastname, CONCAT( firstname, ' ', UPPER(lastname) ) AS fullname, middlename,
		CASE WHEN birthyear IS NULL THEN ''
			ELSE CONCAT( COALESCE(birthday, '--'), '/', COALESCE(birthmonth, '--'), '/', COALESCE(birthyear, '----') ) END AS birthdate,
		CASE WHEN deathyear IS NULL THEN ''
			ELSE CONCAT( COALESCE(deathday, '--'), '/', COALESCE(deathmonth, '--'), '/', COALESCE(deathyear, '----') ) END AS deathdate,
		birthplace, birthorder
	FROM dat_persons
	WHERE id_father=:id_parent XOR id_mother=:id_parent
	ORDER BY birthyear, birthmonth, birthday
	SQL;
	$stmt = $db->prepare($sql);
	$stmt->execute([':id_parent'=>$parentId]);
	return $stmt->fetchAll();
}
 
function getDescent($parentId, $parentGender, $ol='', $tab=-1)
{
	$Data = getChildren($parentId);
 
	//only if at least one child
	if( isset($Data[0]) ) {
		$tab++;
		$indent='';
		for ($i=0; $i<$tab; $i++) {
			$indent .= "\t";
		}
		$ol .= $indent."<ol>\n";
		$indent_li = $indent."\t";
		foreach ($Data as $row) {
			$ol .= "$indent_li<li><a href='parentPage.php?childPageKey=2&amp;idMain={$row['id']}>{$row['fullname']} ({$row['birthdate']} - {$row['deathdate']})</a></li>\n";
			$ol = getDescent($row['id'], $ol, $tab);
		}
		$ol .= $indent."</ol>\n";
	}
	var_dump($ol);	// 0, 0, 0, 0</ol>
	return $ol;
}
//.............................................
 
$idMain = $_SESSION['idMain'];
 
$Data	= getPerson($idMain);
 
$root	= "0. {$Data['fullname']} {$Data['birthdate']} - {$Data['deathdate']}";
$RecursiveList = getDescent($idMain, $Data['gender']);
var_dump($RecursiveList);
Ce code garde la même structure que l'ancien code. J'ai princialement modifié les liens et remplacé le retour des requêtes (tableaux au lieu d'objets). La ligne 48 retourne la chaîne "0, 0, 0, 0</ol>" au lieu de la balise <ol> complète avec ses balises enfants. J'aimerais comprendre ce qui ne va pas.