I'm working on comments for my site. I'm just doing something a little stupid here. I'm getting extra comments. I know it has something to do with my foreach outputting comments foreach item. What's the trick for getting this out once? Thanks, jb. // show comments foreach($retrievedComments as $row) { //display the records here echo '<div class="comment"> <img src="guest/images/girl2.jpg" height="69" width="69"/> <span class="commentUserName"><b>'.htmlentities($row['commenter']).'</b></span> <p style="padding-left:8px;">'.htmlentities($row['comment']).'</p> <div id="commentbutton'.$row['id'].'" class="commentButton"><b class="reply" onclick="reply_comment(' . '\'' . $row['id'] .'\'' . ',' . '\'' . $row['commentToken'] . '\'' . ',' . '\'' . $row['commenter'] . '\'' . ',' . '\'' . $profileOwner->return_username() . '\'' . ')">Reply</b></div> </div>'; //Test if this comment has replies $commentToken = $row['commentToken']; try{ $sqlGetCommentReplies = $db->prepare("SELECT `id`, `userName`, `commenter`, `comment`, " ."`commentToken`, `replyTo` FROM `comments` WHERE `replyTo` = ?"); $sqlGetCommentReplies->execute(array($commentToken)); if($sqlGetCommentReplies->rowCount() > 0) { $retrievedReplies = $sqlGetCommentReplies->fetchAll(); // output comment replies foreach($retrievedReplies as $row2) { echo '<div class="commentreply" style=""> <img src="guest/images/girl2.jpg" height="69" width="69"/> <span class="commentUserName"><b>'.htmlentities($row2['commenter']).'</b></span> <p style="padding-left:8px;">'.htmlentities($row2['comment']).'</p> <div id="commentbutton'.$row2['id'].'" class="commentButton"><b class="reply" onclick="reply_comment(' . '\'' . $row2['id'] .'\'' . ',' . '\'' . $row2['commentToken'] . '\'' . ',' . '\'' . $row2['commenter'] . '\'' . ',' . '\'' . $profileOwner->return_username() . '\'' . ')">Reply</b></div> </div>'; // end of show replies foreach } } }catch(\PDOException $e){} } PHP:
The code should work as you intend - how many rows do you get in the return from the database? And how many should you get?
There's three comments in the db.. "This is main comment" "This is reply to main comment" "This is reply to reply" What I should get is one main comment and two comments under it... but I get is: Main comment, reply to main under it, reply to reply under that.. Then below I get main comment again, and reply to main.... here's a pic http://www.use.com/B3suK Still a little messy, but hope to have someone attack the layout for me this summer.
Shoot, stupid mistake, you're right. This was working perfect.. I have an error somewhere else... my reply was coming out as a regular comment instead of a reply.
For some reason I'm not getting a comment token on replies.. the comment token is the comment token of a main comment... function reply_comment(idSet, tokenSet, recieverSet, profileOwnerSet) { var profileOwner = profileOwnerSet var commentID = idSet; var commentToken = tokenSet; var commentElement = document.getElementById('commentbutton'+commentID); commentElement.innerHTML = ""; commentElement.innerHTML = '<form method="post" action="gateway/code/php/reply_comment.php">' + '<textarea name="comment" cols="90"></textarea>' + '<input type="hidden" name="profileOwner" value="' + profileOwner + '" />' + '<input type="hidden" name="replyToken" value="' + commentToken + '" />' + '<input type="submit" value="comment" />' + '</form>'; Code (JavaScript): reply_comment.php require('../data/sqldata.php'); require('../classes/php/cUser.php'); session_start(); $replyToken = $_POST['replyToken']; $commentingUser = 'guest'; $profileOwner = $_POST['profileOwner']; $comment = $_POST['comment']; $db = new PDO($dsn, $dbUserName, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); if(isset($_SESSION['id'])) { $user = new cUser; $user->set_userID($_SESSION['id']); $commentingUser = $user->return_username(); } try{ $sqlInsertComment = $db->prepare("INSERT INTO `comments`(`userName`, `commenter`, `comment`, " ."`replyTo`) VALUES (?, ?, ?, ?)"); $sqlInsertComment->execute(array($profileOwner, $commentingUser, $comment, $replyToken)); }catch(\PDOException $e){ } PHP: