Bonjour,

Je souhaite faire une boucle pour afficher les commentaires après avoir rempli le formulaire. J'ai essayé ceci mais lorsque je veux faire un autre commentaire il supprime l'ancien, et affiche le nouveau alors que je voudrais afficher chaque commentaires. Il faudrait donc utiliser une boucle mais je ne sais pas quelle boucle utiliser et comment ?
Merci de bien vouloir m'aider.


Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
$name= check_and_secure($_POST, 'comment_name');	
 
  $text = check_and_secure($_POST, 'comment');
 
   echo '<b>'.$name .'</b>'.' à dit... <br /> ';
   echo $text. '<br /> <br /> '. ' <HR align="left" width=15% noshade size=4> ';

Mon fichier 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
<?php
try {
           $connection = new MongoClient();
           $database   = $connection->selectDB('pro');
           $collection = $database->selectCollection('articles');
         } Catch(MongoException $e) {
           die("Failed to connect to database ".$e->getMessage());
}
         $cursor = $collection->find();
 
       ?>
 
<?php session_start();
 
?>
 
<html>
 
<p> this is my article </p>
 
 
<p> blablablablablablablablablablablablablablablablablablablablablablablablablabla </p>
 
 <HR width=80% noshade size=4> 
 
 <h3 />add comment </h3>
 
 
<?php
 
 
 
 
$name= check_and_secure($_POST, 'comment_name');	
 
  $text = check_and_secure($_POST, 'comment');
 
   echo '<b>'.$name .'</b>'.' à dit... <br /> ';
   echo $text. '<br /> <br /> '. ' <HR align="left" width=15% noshade size=4> ';
 
 
?>
 
 
 
   <html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head
</html>
 
 
 
 
              <form action="" method="post">
              <span class="input-label">Name</span>
                   <input type="text" required name="comment_name"
 
                   <br/><br/>
                   <span class="input-label">Email</span>
                   <input type="text" required name="comment_email" 
 
                   <br/><br/>
                   <textarea class="input-label" name="comment" required rows="5" cols="30"></textarea> <br/><br/>
                   <input type="hidden" name="article_id" value="<?php echo $article['_id'];?>" />
                   <input type="submit" name="btn_submit" value="Save"/>
                 </form>
 
 
 
<?php
 
 
function process_comment_if_any_comment_posted ()
{
   $comment = get_posted_comment_if_any_and_secure();
   if(invalid_or_no_comment($comment)) return;
   insert_comment($comment);
}
 
process_comment_if_any_comment_posted();
 
 
 
 
function get_posted_comment_if_any_and_secure()
{
  $comment = array();
 
  if(! comment_posted()) return $comment;
  $article=check_and_secure($_POST, 'article_id');
  $email= check_and_secure($_POST, 'comment_email');
  $name= check_and_secure($_POST, 'comment_name');		
  $text = check_and_secure($_POST, 'comment');
 
  if($text == "") return $comment;
 
  $comment['text'] = $text;
  $comment['comment_name'] = $name;
  $comment['comment_email'] = $email;
  $comment['article'] = $article;
 
 
 
 
 
 
 
  global $article;
 
   $comment['article'] = $article;
 
  return $comment;
}
 
 
 
 
function invalid_or_no_comment($comment)
{
  return empty($comment)
    || ! isSet($comment['text'])
    || (trim($comment['text']) == "");
}
 
 
 
function insert_comment($comment)
{
  global $database;
$database->comments->insert($comment);
  return $comment['_id'];
}
 
function get_article()
{
  $article = array();
 
  return $article;
}
 
$article = get_article();
 
function comment_posted()
{
   return isSet($_POST['article_id']);
}
 
 
 
 
function check_and_secure($T, $field)
{
  if(! isSet($T[$field])) return "";
  return htmlentities($T[$field]);
}