par
am9511 » 12 avr. 2015, 00:38
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 : Tout sélectionner
<?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 "";
return htmlentities($T[$field]);
}
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]<?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 "";
return htmlentities($T[$field]);
}[/code]