Bonjour à tous.
Je suis actuellement sur un problème avec le fetch de PHP.
Je cherche à créer une page nommée activite.php qui regroupe un bon nombre d'informations provenant de ma base de données. Cette page est un genre d'annuaire des activités.
La page se schématise de la façon suivante :
RUBRIQUE :
Activite (Nom association liée)
Description
Contact1
Contact2
Contact3
Activite2 (Nom association liée)
Description
Contact1
Contact2
Contact3
Activite3
.....
Rubrique 2
ect ....
Toutes les informations s'affichent correctement (nom de la rubrique, de l'activité, de l'association, la description, l'email, ...) sauf les contacts.
En effet, je voudrais que les contacts liés aux activités s'affichent (si ils existent) en dessous de chaque activités (comme la description, l'email ou autre) sauf que dans une rubrique le contact affiché sera toujours le même.
Par exemple : On a deux activités dans une rubrique, sur mon site les deux activités auront le même nom de contact or ce n'est pas le cas dans la base de données.
Il doit y avoir potentiellement un problème avec mon fetch (peut-être est-il mal placé, peut-être faut-il le lancer avec une boucle while) mais je n'arrive pas du tout à trouver la façon de réaliser ça correctement...
Voici le code de la page (il se peut qu'il y ai des choses inutiles avec les tests faits) :
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 <?php include 'connectionbdd.php'; ?> <html> <head> <meta charset="utf-8" /> <title>Activité de la ville</title> <link rel="stylesheet" type="text/css" href="stylepage.css"> <!-- Lien vers le css --> </head> <body> <div class="page"> <?php include 'entete2.php'; ?> <div class="content"> <?php // Supprime les données de la table contact_activites $req = $bdd->prepare('DELETE FROM contact_activites'); $req->execute(); $req->closeCursor(); // Récupère les informations pour remplir la table contact_activites $reqtest = $bdd->prepare('SELECT activite.ID as activite_id, contact.ID as contact_id FROM activite, contact WHERE contact.ID = activite.CONTACT1 OR contact.ID = activite.CONTACT2 OR contact.ID = activite.CONTACT3'); $reqtest->execute(); // Ajoute les informations dans la table contact_activites while($contact_activites = $reqtest->fetch()) { $req = $bdd->prepare('INSERT INTO contact_activites (ID_CONTACT, ID_ACTIVITE_ASSOCIE) VALUES (:contact_id, :activite_id)'); $req->execute(array('contact_id' => $contact_activites['contact_id'], 'activite_id' => $contact_activites['activite_id'])); $req->closeCursor(); } // Récupère les rubriques (seulement celle qui ont des activités associées) + classement par ordre alphabétique $req = $bdd->prepare('SELECT DISTINCT rubrique.LIBELLE as rubrique, rubrique.ID as rubrique_id FROM activite, rubrique WHERE activite.RUBRIQUE = rubrique.ID ORDER BY rubrique'); $req->execute(); // Récupération des données à afficher : nom de l'activité, de l'association, siteweb, tarif, email, description et les contacts $req2 = $bdd->prepare('SELECT DISTINCT activite.LIBELLE as activite, association.LIBELLE as association, activite.SITEWEB as siteweb, activite.EMAIL as email, activite.DESCRIPTION as description, activite.TARIF_ACTIVITE as tarif, contact_activites.ID_CONTACT as id_contact FROM activite INNER JOIN association ON (activite.ASSOCIATION = association.ID) INNER JOIN rubrique ON (activite.RUBRIQUE = rubrique.ID) LEFT JOIN contact_activites ON (activite.ID = contact_activites.ID_ACTIVITE_ASSOCIE) LEFT JOIN contact ON (contact.ID = contact_activites.ID_CONTACT) WHERE activite.RUBRIQUE = :rubrique_id'); $req3 = $bdd->prepare('SELECT DISTINCT contact.NOM as contact, contact.PRENOM as contact_prenom, contact.ID as id_contact FROM activite INNER JOIN association ON (activite.ASSOCIATION = association.ID) INNER JOIN rubrique ON (activite.RUBRIQUE = rubrique.ID) LEFT JOIN contact_activites ON (activite.ID = contact_activites.ID_ACTIVITE_ASSOCIE) LEFT JOIN contact ON (contact.ID = contact_activites.ID_CONTACT) WHERE activite.RUBRIQUE = :rubrique_id'); $req_contact = $bdd->prepare('SELECT DISTINCT * FROM contact WHERE ID = :contact_id'); $req_activite = $bdd->prepare('SELECT * FROM activite WHERE RUBRIQUE = :rubrique_id'); ?><h1 class="titre_basic"> Activité de la ville de Saint-Martin-Lez-Tatinghem </h1> <?php // Tant qu'on a pas traité toutes les rubriques utilisées while($rubrique_use = $req->fetch()) { ?> <h2 class='h2_rubrique'> <?php echo $rubrique_use['rubrique']; ?> </h2> <?php $req2->bindValue(':rubrique_id', $rubrique_use['rubrique_id'], PDO::PARAM_STR); $req2->execute(); $nom_activite_courante = ''; // initialisation while($libelle = $req2->fetch()) { ?> <div id="info_activites"> <?php // Test permettant d'éviter les doublons des informations if($libelle['activite']!=$nom_activite_courante) { $nom_activite_courante = $libelle["activite"]; // Affichage des informations d'une "nouvelle activité" : // Affichage NOM activité (NOM association) $activite_association = '<span style="color:rgb(241, 113, 113)">' . $libelle['activite'] . '</span>'; // Si le nom de l'activité est différent du nom de l'association : on affiche les deux if($libelle['activite']!=$libelle['association']) { $activite_association = $activite_association . ' (' . '<span style="font-weight:bold">' . $libelle['association'] . '</span>' . ')' . '</br>'; } // Sinon on ne l'affiche pas else { $activite_association = $activite_association . '</br>'; } echo $activite_association; // Test SITEWEB if($libelle['siteweb']!=NULL) { $siteweb = 'Site web : ' . $libelle['siteweb'] . '</br>'; echo $siteweb; } // Test EMAIL if($libelle['email']!=NULL) { $email = 'Adresse email : ' . $libelle['email'] . '</br>'; echo $email; } // Test DESCRIPTION if($libelle['description']!=NULL && $libelle['description']!=" ") { $description = 'Description : ' . $libelle['description'] . '</br>'; echo $description; } // Test TARIF_ACTIVITE if($libelle['tarif']!=0 && $libelle['tarif']!=NULL) { $tarif = 'Tarif de l\'activité : ' . $libelle['tarif'] . ' </br>'; echo $tarif; } $req_activite->bindValue(':rubrique_id', $rubrique_use['rubrique_id'], PDO::PARAM_STR); $req_activite->execute(); $donnees_activite = $req_activite->fetch(); $req_contact->bindValue(':contact_id',$donnees_activite['CONTACT1'], PDO::PARAM_STR); $req_contact->execute(); $donnees_contact1 = $req_contact->fetch(); if($donnees_activite['CONTACT1']>0) { echo $donnees_contact1['NOM']." ".$donnees_contact1['PRENOM']."<br>"; } $req_contact->bindValue(':contact_id', $donnees_activite['CONTACT2'], PDO::PARAM_STR); $req_contact->execute(); $donnees_contact2 = $req_contact->fetch(); if($donnees_activite['CONTACT2']>0) { echo $donnees_contact2['NOM']." ". $donnees_contact2['PRENOM']; } } ?> </div> </div><?php } } ?> </div> <br> <br> <?php include 'footer.php'; ?> </div> </body> </html>
Je rajoute aussi le contenu de la table activite :
Ainsi que le code de la BDD :
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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 -- -- Structure de la table `activite` -- CREATE TABLE IF NOT EXISTS `activite` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `LIBELLE` varchar(255) DEFAULT NULL, `SITEWEB` varchar(255) DEFAULT NULL, `EMAIL` varchar(255) DEFAULT NULL, `DESCRIPTION` varchar(300) DEFAULT NULL, `TARIF_ACTIVITE` int(11) DEFAULT NULL, `RUBRIQUE` varchar(255) NOT NULL, `ASSOCIATION` varchar(255) NOT NULL, `CONTACT1` int(5) DEFAULT NULL, `CONTACT2` int(5) DEFAULT NULL, `CONTACT3` int(5) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ; -- -- Contenu de la table `activite` -- ----------------------------------------------- -- -- Structure de la table `association` -- CREATE TABLE IF NOT EXISTS `association` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `LIBELLE` varchar(255) NOT NULL, `TARIF_ADHESION` int(11) DEFAULT NULL, `PRESIDENT` int(5) DEFAULT NULL, `TRESORIER` int(5) DEFAULT NULL, `SECRETAIRE` int(5) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=57 ; -- -- Contenu de la table `association` -- -- -------------------------------------------------------- -- -- Structure de la table `contact` -- CREATE TABLE IF NOT EXISTS `contact` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `NOM` varchar(255) NOT NULL, `PRENOM` varchar(255) NOT NULL, `NUMTEL` varchar(12) DEFAULT NULL, `NUMTELMOBIL` varchar(12) DEFAULT NULL, `EMAIL` varchar(255) DEFAULT NULL, `ADRESSE` varchar(255) DEFAULT NULL, `CP` int(6) DEFAULT NULL, `VILLE` varchar(255) DEFAULT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=68 ; -- -- Contenu de la table `contact` -- -- -------------------------------------------------------- -- -- Structure de la table `contact_activites` -- CREATE TABLE IF NOT EXISTS `contact_activites` ( `ID_CONTACT` int(11) NOT NULL, `ID_ACTIVITE_ASSOCIE` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Contenu de la table `contact_activites` -- -- -------------------------------------------------------- -- -- Structure de la table `membres` -- CREATE TABLE IF NOT EXISTS `membres` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `PSEUDO` varchar(255) DEFAULT NULL, `MDP` varchar(255) DEFAULT NULL, `ID_ROLE` int(5) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ; -- -- Contenu de la table `membres` -- -- -------------------------------------------------------- -- -- Structure de la table `role` -- CREATE TABLE IF NOT EXISTS `role` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `ROLE` varchar(255) NOT NULL, PRIMARY KEY (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ; -- -------------------------------------------------------- -- -- Structure de la table `rubrique` -- CREATE TABLE IF NOT EXISTS `rubrique` ( `ID` int(11) NOT NULL AUTO_INCREMENT, `LIBELLE` varchar(255) NOT NULL, KEY `ID` (`ID`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=12 ;
Pour plus d'information, je reste disponible.
SMVE
Partager