par
nico44530 » 17 mars 2013, 21:16
Bonjour,
Je suis en création d'un système de commentaire à partir de news (post).
Voilà comment j'affiche mes news :
<?php
try{
$bdd = new PDO('mysql:host=***;dbname=***', '***', '***');
}
catch (PDOException $e){
die('Erreur : ' . $e->getMessage());
}
$obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date_heure ASC;");
$req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY id_com ASC;");
$is_empty = true;
while ($data = $obj->fetch())
{
$is_empty = false;
?>
<li class="user-item" style="float: left;padding: 13px 10px 10px;width: 460px;border-top: 1px solid #cecece;word-wrap: break-word;">
<div class="user-avatar float-l" style="margin-right: 10px;">
<a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>"><img src="../img/upload/<?php echo htmlentities(trim($data['id'])); ?>-mini.png" width="50" height="50" alt="" /></a>
</div>
<div class="user-title" style="margin-bottom: 5px;font-weight: bold;">
<a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>" style="color: #005B83;"><?php echo htmlentities(trim($data['prenom'])); ?> <?php echo htmlentities(trim($data['nom'])); ?></a>
</div>
<div class="user-content" style="margin-left: 60px;">
<p><?php echo htmlentities(nl2br(trim($data['poster']))); ?></p>
</div>
<div class="sub-userContent" style="margin-left: 60px;">
<div class="sub-userFooter" style="margin-top: 6px;">
<p class="size11" style="color: #777;"><a href="" class="sub-userFooter-link" style="color: #0D5085;">J'aime</a> - <a href="" class="sub-userFooter-link" style="color: #0D5085;">Commenter</a> - <?php echo getRelativeTime($data['date_heure']); ?></p>
</div>
<ul class="sub-userData" style="list-style-type: none;">
<li>
<i class="sub-userArrow" style="background: url(../img/top-sub-user.png);background-repeat: no-repeat;height: 5px;display: block;margin-top: 5px;margin-left: 60px;"></i>
</li>
<?php
$req->execute(array('id'=>$data['id_post']));
while ($data_comm = $req->fetch())
{ ?>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<div class="sub-userCom" style="overflow: hidden;padding-left: 4px;">
<div>
<p class="size11">
<a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;">
<b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a>
<span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span>
</p>
</div>
<div>
<p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p>
</div>
</div>
</li>
<?
}
?>
<div class="<?php echo $data['id']; ?>"></div>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $result['id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<form action="" method="post" onsubmit="return false;" class="form_com">
<input type="hidden" name="post_id" value="<?php echo $data['id_post']; ?>" />
<textarea name="msg_com" class="<?php echo $data['pseudo']; ?>" style="border: 1px solid #999;font-size: 11px; margin-left: 5px; height: 26px; padding: 5px; width: 343px;resize: none;font-family: Tahoma;position: relative;overflow: hidden;display: block;" id="<?php echo $data['id_post']; ?>" placeholder="Écrire un commentaire"></textarea>
</form>
<script type="text/javascript">
var J = jQuery.noConflict();
J(document).ready(function(){
J(".form_com").keypress(function(e) {
if(e.which == 13) {
var <?php echo $data['pseudo']; ?> = J('.<?php echo $data['pseudo']; ?>').val();
if( <?php echo $data['pseudo']; ?> == '') {
return false;
} else {
J('span.load-refresh').show();
setTimeout(function() {
J.ajax({
url: "ajax/add_comment.php",
type: "POST",
data: J(this).serialize(),
success: function(html) {
J('.<?php echo $data['id']; ?>').append(html);
J('.<?php echo $data['pseudo']; ?>').val('');
J('span.load-refresh').hide();
return false;
}
});
}, 1000);
return false;
}
}
});
});
</script>
<script type="text/javascript">window.addEvent('domready', function() {new DynamicTextarea('<?php echo $data['id_post']; ?>');});</script>
</li>
</ul>
</div>
</li>
<?php
$req->closeCursor();
if ($is_empty)
echo '<div style="text-align: center;font-size: 11px;color: #666;">Aucune actualités pour l\'instant</div>';
}
?>
Le problème c'est quand ajax interroge add_comment.php, l'erreur est un peu farfelu :
- J'écris un commentaire dans un premier post.
- Je récris un deuxième commentaire dans le même post
- Le commentaire rentre bien dans ma base de donnée mais à l'affichage en ajax, c'est toujours le premier commentaire qui s'affiche.
- Si maintenant je veux écrire un commentaire dans un autre post, ça va afficher le premier commentaire du premier post
- Si on recharge la page manuellement, tout s'affiche normalement.
Voici le code de la page interrogé :
<?php
session_start();
require('../include/functions.inc.php');
$sql = connect_sql();
$pseudo_membre = $_SESSION['pseudo'];
$requete = mysql_query("SELECT * FROM wa_communaute WHERE pseudo='".$pseudo_membre."'");
$result = mysql_fetch_assoc ($requete);
$post_id = $_POST['post_id'];
$msg_com = $_POST['msg_com'];
if(get_magic_quotes_gpc()) {
$msg_com = stripslashes($msg_com);
}
mysql_query('INSERT INTO wa_comment (id_com, membre_id, msg_com, post_id, date) values("", "'.$_SESSION['id'].'", "'.mysql_real_escape_string(trim(htmlentities($msg_com, ENT_QUOTES, 'UTF-8'))).'", "'.$post_id.'", "now()")');
mysql_close();
try{
$bdd = new PDO('mysql:host=****;dbname=****', '****', '****');
}
catch (PDOException $e){
die('Erreur : ' . $e->getMessage());
}
$obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date_heure DESC;");
$req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY date DESC;");
$data = $obj->fetch();
$req->execute(array('id'=>$data['id_post']));
$data_comm = $req->fetch();
?>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<div class="sub-userCom" style="overflow: hidden;padding-left: 4px;">
<div>
<p class="size11">
<a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;">
<b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a>
<span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span>
</p>
</div>
<div>
<p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p>
</div>
</div>
</li>
<?php
$req->closeCursor();
$obj->closeCursor();
?>
Merci d'avance

Bonjour,
Je suis en création d'un système de commentaire à partir de news (post).
Voilà comment j'affiche mes news :
[php]
<?php
try{
$bdd = new PDO('mysql:host=***;dbname=***', '***', '***');
}
catch (PDOException $e){
die('Erreur : ' . $e->getMessage());
}
$obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date_heure ASC;");
$req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY id_com ASC;");
$is_empty = true;
while ($data = $obj->fetch())
{
$is_empty = false;
?>
<li class="user-item" style="float: left;padding: 13px 10px 10px;width: 460px;border-top: 1px solid #cecece;word-wrap: break-word;">
<div class="user-avatar float-l" style="margin-right: 10px;">
<a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>"><img src="../img/upload/<?php echo htmlentities(trim($data['id'])); ?>-mini.png" width="50" height="50" alt="" /></a>
</div>
<div class="user-title" style="margin-bottom: 5px;font-weight: bold;">
<a href="/<?php echo htmlentities(trim($data['pseudo'])); ?>" style="color: #005B83;"><?php echo htmlentities(trim($data['prenom'])); ?> <?php echo htmlentities(trim($data['nom'])); ?></a>
</div>
<div class="user-content" style="margin-left: 60px;">
<p><?php echo htmlentities(nl2br(trim($data['poster']))); ?></p>
</div>
<div class="sub-userContent" style="margin-left: 60px;">
<div class="sub-userFooter" style="margin-top: 6px;">
<p class="size11" style="color: #777;"><a href="" class="sub-userFooter-link" style="color: #0D5085;">J'aime</a> - <a href="" class="sub-userFooter-link" style="color: #0D5085;">Commenter</a> - <?php echo getRelativeTime($data['date_heure']); ?></p>
</div>
<ul class="sub-userData" style="list-style-type: none;">
<li>
<i class="sub-userArrow" style="background: url(../img/top-sub-user.png);background-repeat: no-repeat;height: 5px;display: block;margin-top: 5px;margin-left: 60px;"></i>
</li>
<?php
$req->execute(array('id'=>$data['id_post']));
while ($data_comm = $req->fetch())
{ ?>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<div class="sub-userCom" style="overflow: hidden;padding-left: 4px;">
<div>
<p class="size11">
<a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;">
<b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a>
<span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span>
</p>
</div>
<div>
<p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p>
</div>
</div>
</li>
<?
}
?>
<div class="<?php echo $data['id']; ?>"></div>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $result['id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<form action="" method="post" onsubmit="return false;" class="form_com">
<input type="hidden" name="post_id" value="<?php echo $data['id_post']; ?>" />
<textarea name="msg_com" class="<?php echo $data['pseudo']; ?>" style="border: 1px solid #999;font-size: 11px; margin-left: 5px; height: 26px; padding: 5px; width: 343px;resize: none;font-family: Tahoma;position: relative;overflow: hidden;display: block;" id="<?php echo $data['id_post']; ?>" placeholder="Écrire un commentaire"></textarea>
</form>
<script type="text/javascript">
var J = jQuery.noConflict();
J(document).ready(function(){
J(".form_com").keypress(function(e) {
if(e.which == 13) {
var <?php echo $data['pseudo']; ?> = J('.<?php echo $data['pseudo']; ?>').val();
if( <?php echo $data['pseudo']; ?> == '') {
return false;
} else {
J('span.load-refresh').show();
setTimeout(function() {
J.ajax({
url: "ajax/add_comment.php",
type: "POST",
data: J(this).serialize(),
success: function(html) {
J('.<?php echo $data['id']; ?>').append(html);
J('.<?php echo $data['pseudo']; ?>').val('');
J('span.load-refresh').hide();
return false;
}
});
}, 1000);
return false;
}
}
});
});
</script>
<script type="text/javascript">window.addEvent('domready', function() {new DynamicTextarea('<?php echo $data['id_post']; ?>');});</script>
</li>
</ul>
</div>
</li>
<?php
$req->closeCursor();
if ($is_empty)
echo '<div style="text-align: center;font-size: 11px;color: #666;">Aucune actualités pour l\'instant</div>';
}
?>
[/php]
Le problème c'est quand ajax interroge add_comment.php, l'erreur est un peu farfelu :
- J'écris un commentaire dans un premier post.
- Je récris un deuxième commentaire dans le même post
- Le commentaire rentre bien dans ma base de donnée mais à l'affichage en ajax, c'est toujours le premier commentaire qui s'affiche.
- Si maintenant je veux écrire un commentaire dans un autre post, ça va afficher le premier commentaire du premier post
- Si on recharge la page manuellement, tout s'affiche normalement.
Voici le code de la page interrogé :
[php]
<?php
session_start();
require('../include/functions.inc.php');
$sql = connect_sql();
$pseudo_membre = $_SESSION['pseudo'];
$requete = mysql_query("SELECT * FROM wa_communaute WHERE pseudo='".$pseudo_membre."'");
$result = mysql_fetch_assoc ($requete);
$post_id = $_POST['post_id'];
$msg_com = $_POST['msg_com'];
if(get_magic_quotes_gpc()) {
$msg_com = stripslashes($msg_com);
}
mysql_query('INSERT INTO wa_comment (id_com, membre_id, msg_com, post_id, date) values("", "'.$_SESSION['id'].'", "'.mysql_real_escape_string(trim(htmlentities($msg_com, ENT_QUOTES, 'UTF-8'))).'", "'.$post_id.'", "now()")');
mysql_close();
try{
$bdd = new PDO('mysql:host=****;dbname=****', '****', '****');
}
catch (PDOException $e){
die('Erreur : ' . $e->getMessage());
}
$obj = $bdd->query("SELECT * FROM wa_mur, wa_communaute WHERE id_membre = wa_communaute.id ORDER BY date_heure DESC;");
$req = $bdd->prepare("SELECT * FROM wa_comment, wa_communaute WHERE post_id = :id AND membre_id = wa_communaute.id ORDER BY date DESC;");
$data = $obj->fetch();
$req->execute(array('id'=>$data['id_post']));
$data_comm = $req->fetch();
?>
<li class="sub-userItem" style="background-color: #bebebe;margin: 0;padding: 4px;margin: 0 0 1px;min-height: 32px;">
<div class="user-avatarCom" style="float: left;padding: 0;margin: 0;">
<img style="margin-right: 4px;" src="../img/upload/<?php echo $data_comm['membre_id']; ?>-mini.png" width="32" height="32" alt="" />
</div>
<div class="sub-userCom" style="overflow: hidden;padding-left: 4px;">
<div>
<p class="size11">
<a href="/<?php echo htmlentities(trim($data_comm['pseudo'])); ?>" style="color: #005B83;">
<b><?php echo htmlentities(trim($data_comm['prenom'])); ?> <?php echo htmlentities(trim($data_comm['nom'])); ?></b></a>
<span> <?php echo nl2br(htmlspecialchars($data_comm['msg_com'])); ?></span>
</p>
</div>
<div>
<p class="size11"><span style="color: #666;display: block;margin-top: 4px;"><?php echo getRelativeTime($data_comm['date']); ?></span></p>
</div>
</div>
</li>
<?php
$req->closeCursor();
$obj->closeCursor();
?>
[/php]
Merci d'avance ;)