Bonjour à tous,
Je voudrais réunir deux fonctions appelées successivement par une seule en incorporant une requête dans la clause WHERE. Il me manque quelque chose avec une variable MySQL que je n'arrive pas à intégrer.
Ancien code à simplifier:
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
// Recherche des identifiants des conjoints avec lesquels la personne principale a eu des enfants
function getSpousesWithCommonChildren($mainPersonId, $mainPersonGender)
{
	global $db;
 
	if ($mainPersonGender == 'M')
	{
		$query = "
			SELECT DISTINCT mother_id AS id
			FROM dat_persons
			WHERE father_id=:mainPersonId
		;";
	}
	if ($mainPersonGender == 'F')
	{
		$query = "
			SELECT DISTINCT father_id AS id
			FROM dat_persons
			WHERE mother_id=:mainPersonId
		;";
	}
	$result = $db->prepare($query);
	$result->bindParam('mainPersonId', $mainPersonId);
	$result->execute();
	$idsStack = '';
	while ($id = $result->fetch())
	{
		$idsStack .= $id->id.',';
	}
	return $idsStack;
}
 
// Recherche des options pour lesquelles on connait les identifiants
function getOptionsDataFromIdsStack(string $idsStack)
{
	global $db;
	$idsStack = trim($idsStack, ", \t\n\r\0\x0B");
 
	$query = OPTIONS_SELECT."
		FROM dat_persons
		WHERE id IN($idsStack)
		ORDER BY last_name, first_name, birth_date
	;";
	$result = $db->query($query);
	return $result->fetchAll();
}
 
$idsStack	= getSpousesWithCommonChildren($mainPerson->id, $mainPerson->gender);
$persons	= getOptionsDataFromIdsStack($idsStack);
Essai de nouveau code:
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
function getSpousesWithCommonChildren($mainPersonId, $mainPersonGender)
{
	global $db;
 
	if ($mainPersonGender == 'M')
	{
		$where = "
			SELECT DISTINCT mother_id AS id
			FROM dat_persons
			WHERE father_id=:mainPersonId
		";
	}
	if ($mainPersonGender == 'F')
	{
		$where = "
			SELECT DISTINCT father_id AS id
			FROM dat_persons
			WHERE mother_id=:mainPersonId
		";
	}
	$query = OPTIONS_SELECT."
		FROM dat_persons
		WHERE ($where)
		ORDER BY last_name, first_name, birth_date
	;";
	$result = $db->prepare($query);
	$result->bindParam('mainPersonId', $mainPersonId);
	$result->execute();
	return $result->fetchAll();
}
 
$persons = getSpousesWithCommonChildren($mainPerson->id, $mainPerson->gender);