Bonjour,
J'ai une table relation n n qui associe une playlist à une ou plusieurs vidéos :
La première colonne fait référence à la table playlist et la deuxième à la table vidéos.
Pour récupérer mes informations, j'écris cette requête :
Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 SELECT p.name as playlist_name, p.created, v.name as video_name, v.thumbnail, v.description, v.posted, v.pseudo FROM relations as r LEFT JOIN playlists as p ON r.id_playlist = p.id LEFT JOIN videos as v ON r.id_video = v.id
Pour lire ses informations, j'écris ce bout de 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 $relations_arr = array(); $relations_arr["playlists"] = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { extract($row); $playlists_item = array( "name_of_playlist" => $row['playlist_name'], "created" => $row['created'], "videos" => array( "name_of_video" => $row['video_name'], "thumbnail" => $row["thumbnail"], "description" => $row["description"], "posted" => $row["posted"], "pseudo" => $row["pseudo"] )); array_push($relations_arr["playlists"], $playlists_item); } http_response_code(200); print_r(json_encode($relations_arr));
Et comme résultat, j'obtiens ceci :
Code json : 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 { "playlists": [ { "name_of_playlist": "Playlist 01", "created": "2018-10-15", "videos": { "name_of_video": "Vidéo 01", "thumbnail": "Lien 01", "description": "Description 01", "posted": "2018-10-31", "pseudo": "Pseudo 01" } }, { "name_of_playlist": "Playlist 01", "created": "2018-10-15", "videos": { "name_of_video": "Vidéo 02", "thumbnail": "Lien 02", "description": "Description 02", "posted": "2018-10-24", "pseudo": "Pseudo 02" } }, { "name_of_playlist": "Playlist 02", "created": "2018-10-03", "videos": { "name_of_video": "Red Dead Redemption 2 - Bande-annonce de lancement", "thumbnail": "https://www.dailymotion.com/video/x6voudz", "description": "Un bon jeu auquel je ne peux pas encore jouer car j'effectue ce test.", "posted": "2018-10-15", "pseudo": "Gamekult" } }, { "name_of_playlist": "Playlist 02", "created": "2018-10-03", "videos": { "name_of_video": "Les chansons pour enfants décryptées ! (feat. SWANN PERISSE) - Parlons peu Mais parlons", "thumbnail": "https://www.dailymotion.com/video/x6v7fv1", "description": "Pour fêter les 3 ans de la chaîne on voulait faire une super fête ! A la place on a chanté des chansons dégueulasses.", "posted": "2018-10-10", "pseudo": "Parlons Peu Mais Parlons" } } ] }
Or, le résultat escompté n'est pas bon, en effet j'aimerai avoir ceci :
Code json : 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 { "playlists": [ "name_of_playlist": "Playlist 01", "created": "2018-10-15": [ "videos_01": { "name_of_video": "Vidéo 01", "thumbnail": "Lien 01", "description": "Description 01", "posted": "2018-10-31", "pseudo": "Pseudo 01" } "videos_02": { "name_of_video": "Vidéo 02", "thumbnail": "Lien 02", "description": "Description 02", "posted": "2018-10-24", "pseudo": "Pseudo 02" } ] "name_of_playlist": "Playlist 02", "created": "2018-10-03", [ "videos_01": { "name_of_video": "Red Dead Redemption 2 - Bande-annonce de lancement", "thumbnail": "https://www.dailymotion.com/video/x6voudz", "description": "Un bon jeu auquel je ne peux pas encore jouer car j'effectue ce test.", "posted": "2018-10-15", "pseudo": "Gamekult" } "videos_02": { "name_of_video": "Les chansons pour enfants décryptées ! (feat. SWANN PERISSE) - Parlons peu Mais parlons", "thumbnail": "https://www.dailymotion.com/video/x6v7fv1", "description": "Pour fêter les 3 ans de la chaîne on voulait faire une super fête ! A la place on a chanté des chansons dégueulasses.", "posted": "2018-10-10", "pseudo": "Parlons Peu Mais Parlons" } ] ] }
Un seul tableau de playlist qui contient ses propres vidéos donc.
Ou qu'elle serait le résultat le plus judicieux ? Sachant que je dois pouvoir par la suite supprimer une vidéo d'une playlist ou en ajouter.
Merci pour vos conseils !
Partager