bonjour tous le monde debutant en php et j'ai voulu realiser un livre d'or avec l'architecture MVC en me basant sur differents exples j'ai creer ma base de donnée avec les 2 tables signataire et relation .3 dossiers modele vue et controleur apres avoir remplit le formulaire rien ne s'affiche au niveau de la page d'affichage
dossier modele avec 3 class
Code php :
dossier vue avec 2 class liste et formulaire
Code : 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 <?php class Database{ private $serveur; private $bdmane; private $user; private $password; private $instance = null; public function __construct($serveur='localhost',$bdmane='livre', $user='root', $password='') { $this->instance = new PDO('mysql:host=localhost;dbname=livre;charset=utf8','root',''); $this->serveur =$serveur; $this->bdmane =$bdmane; $this->user =$user; $this->password =$password; } public function connexion(){ $this->pdo = new PDO('mysql:host=localhost;dbname=livre;charset=utf8','root',''); return $this; } public static function getInstance() { if(is_null(self::$_instance)) { self::$_instance = new database(); } return self::$_instance; } } <?php class Signataire{ private $Civilite=''; private $NomComplet=''; private $Email=''; private $Pseudo=''; private $Anonymat; private $Message=''; private $Photo=''; private $relation= array(); function __construct($g = null){ if(is_array($g)) foreach($g as $k => $v) if(property_exists($this, $k)){ if($k == 'relation'){ if(!is_array($v)) $v = explode(';',$v); } $this->$k = $v; } } function __get($k){ if(property_exists($this, $k)) return $this->$k;} public function Civilite(){ return $this->Civilite; } public function NomComplet(){ return $this->NomComplet; } public function Email(){ return $this->Email; } public function Pseudo(){ return $this->Pseudo; } public function Anonymat(){ return $this->Anonymat; } public function Message(){ return $this->Message; } public function Photo(){ return $this->Photo; } function setPhoto($f){ $this->photo = $f; } static function lister(){ //connexion $pdo = new PDO('mysql:host=localhost;dbname=livre;charset=utf8','root',''); if($id===null){ //requete preparee $q = $pdo->prepare('select * from signataire'); //execution avec le tableau contenant les valeurs des parametres $q->execute(); //parcours avec fetch; faire du setFetchMode pour retourner des objets persos //$q->setFetchMode(PDO::FETCH_CLASS,__CLASS__); $list = array(); while($obj=$q->fetch(PDO::FETCH_ASSOC)){ //$obj->hobbies = explode(';',$obj->hobbies); //$obj->comp = explode(';',$obj->comp); $list[] = new signataire($obj); } } else { //requete preparee $q = $pdo->prepare('select * from signataire where id = ?'); //execution avec le tableau contenant les valeurs des parametres $q->execute(array($id)); $obj=$q->fetch(PDO::FETCH_ASSOC); $list = new signataire($obj); } $pdo = null; return $list; } function sauvegarder(){ //connexion $pdo = new PDO('mysql:host=localhost;dbname=livre;charset=utf8','root',''); //requete preparee $data = array( ':Civilite' => $obj1->Civilite(), ':NomComplet' => $obj1->NomComplet(), ':Email' => $obj1->Email(), ':Pseudo' => $obj1->Pseudo(), ':Anonymat' => $obj1->Anonymat(), ':Message' => $obj1->Message(), ':Relation' => implode(';',$this->Relation()), ':Photo' => $obj1->Photo()); //var_dump($this);exit; if(!$this->id){ $q = $pdo->prepare('insert into signataire(Civilite,NomComplet,Email,Pseudo,Anonymat,Message,Relation,Photo) values(:Civilite, :NomComplet, :Email, :Pseudo, :Anonymat, :Message, :Relation, :Photo)'); } else{ $q = $pdo->prepare('update signataire set Civilite=:Civilite, NomComplet=:NomComplet, Email=:Email, Pseudo=:Pseudo, Anonymat=:Anonymat, Message=:Message, Relation=:Relation, Photo=:Photo'); $data['id'] = $this->id; } //execution avec le tableau contenant les valeurs des parametres $q->execute($data); //var_dump($q->errorInfo()); //libére la connexion $pdo = null; } } ?> <?php class Relation{ private $relation ; public function__construct($option){ $this->option = $option; } if(is_array($val)){ foreach($val as $k -> $value){ if(property_exists($this, $k)){ $this -> $k = $value; } } } } public function relation(){ return $this -> relation; } //public function__construct(){ affiche une erreur ?>
affiche le formulaire mais une erreur est apparue à la ligne 8
le formulaire
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1- <?php foreach($list as $v){?><div> 2- <?php echo $v -> Message();?></p> <p> 3- <?php if ($v -> Anonymat()==true){?><p> 4- <?php echo $v -> Pseudo ();?></p> <p> 5- <?php {?><div> 6- <?php else {?><div></p> 7- <?php echo $v -> Message();?></p> 8- <div><img src="<?php echo $v -> Photo():?> 9- <?php echo $v -> nom();?></div> 10- </div> 11- <?php{?> 12- <a href="livre-d-or.php?id=0">Ajouter un individu</a>
controleur:livre-d-or.php
Code : 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 <?php global $obj; ?> <?php require_once("template/entete.html"); ?> <!DOCTYPE HTML> <head> <meta charset="utf-8"> <style> body{ width:900px; margin: auto; } header img{ vertical-align: middle; margin-right:50px; } h1{ font-size: 3.5em; color: #fff; text-shadow: 4px 4px 4px black; font-family: sans-serif; } nav a{ color:#aaaaaa; } nav ul li { font-family: sans-serif; border-radius: 5px 5px; background-color: blue; padding: 5px; text-decoration: none; display:inline-block; margin-top: 20px; margin-bottom: 30px; margin-left:15px; margin-right:15px; font-size: 1.5em; } nav{ margin-left:140px; } article{ width:70%; text-align: justify 23%; margin:20px; } article p{ text-align: center 0.9em; font-family: arial 14px; } h2{ color:blue ; } article img{ float: left; width-min: 100px; width-max: 200px; } #main { float:left; width:75%; } #sidebar { float:right; width:22%; } audio{ margin-top: 30px; width:100%; } footer { float: right; padding: 10px; padding-bottom:10px; font-size: 0.9em; } footer a{ color: #aaaaaa; } form{ width: 700px; margin: 15px auto; background-color: blue; color: white; padding: 10px; } label{ display:block; width:120px; margin:0 10px 10px 0; text-align: right; float: left; clear: both; } label.radio, label.checkbox{ width: auto; clear: none; font-style: italic; } input, select{ display: block; float: left; margin-bottom:10px; } input[type="submit"]{ margin-left: 130px; padding: 10px; background-color: white; color: black; clear: both; } input[type="file"],select{ width: 125px; height:125px; background: #CCCCCC; border: 1px solid #FFFFFF; } input[type="varchar"],select{ width: 200px; background: #CCCCCC; border: 1px solid #FFFFFF; } input[type="boolean"],select{ width: 200px; background: #CCCCCC; border: 1px solid #FFFFFF; margin: 10px; } textarea{ margin-right: 150px; padding: 30px; margin-top :25px; } </style> <form action="../modele/database.php" method="post" enctype="multipart/form-data"> <label>Civilite : </label> <input name="civ" type="radio" value="M." /><label class="radio">Monsieur</label> <input name="civ" type="radio" value="Mme" /><label class="radio">Madame</label> <input name="civ" type="radio" value="Mlle" /><label class="radio">Mademoiselle</label> <label>NomComplet : </label> <input name="Nom Complet" type="varchar" /> <label>Email : </label> <input name="Email" type="varchar" /> <label>Pseudo : </label> <input name="Pseudo" type="varchar" /> <label>Anonymat : </label> <input name="Anonymat " type="checkbox" type="booleen" /> <label>Message : </label> <textarea name="message" rows="8" cols="45"> Votre message ici. </textarea> <label>Relation :</label> <input name="Relation[]" value="L" type="checkbox" /><label class="checkbox">Parent</label> <input name="Relation[]" value="V" type="checkbox" /><label class="checkbox">Collegue</label> <input name="Relation[]" value="H" type="checkbox" /><label class="checkbox">Camarade de classe</label> <input name="Relation[]" value="P" type="checkbox" /><label class="checkbox">Ami</label> <input name="Relation[]" value="M" type="checkbox" /><label class="checkbox">Concurrent</label> <input name="Relation[]" value="S" type="checkbox" /><label class="checkbox">Autres</label> <label>Photo : </label> <input name="photo" type="file" /> <input type="submit" /> <br style="clear:both" /> </form> </body> </html> <?php require_once("template/pied.html"); ?>
Code : 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 <?php require_once('../model/database.php'); if(isset($_REQUEST['nom'])) { $obj = new signataire($_REQUEST); //traitrement du fichier via $_FILES if(move_uploaded_file($_FILES['photo']['tmp_name'],'photo/'.$_FILES['photo']['name'])) $obj->setPhoto('photo/'.$_FILES['photo']['name']); $obj->sauvegarder(); } //si $obj est null, afficher le formulaire, sinon afficher la liste $obj = null; if(isset($_GET['id'])){ $obj = signataire::lister($_GET['id']); } ?> <?php if(!$obj){ $list = signataire::lister(); require_once('../vue/liste.php'); } else { include("../livre d'Or.html"); } ?>
Partager