bonsoir,

Je souhaite afficher les commentaires sur un article mais je n’y arrive pas voici mon code php, ma base de donnée est mongodb Merci de bien vouloir m'aider


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
 
<?php
try {
           $connection = new MongoClient();
           $database   = $connection->selectDB('projet');
           $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 comments </h3>
 
      <?php foreach($article['comments'] as $comment):
                echo  $comment['name'].' à dit...';?>
              <p><?php echo $comment['text']; ?></p>
 
 
 
              <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" />
                   <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;
 
  $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;
 
 
 
 
 
  global $article;
 
  $comment['article'] = $article['_id'];
 
  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 "";
  returnhtmlentities($T[$field]);
}