1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with comments

Discussion in 'PHP' started by Jeremy Benson, Apr 8, 2015.

  1. #1
    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:
     
    Jeremy Benson, Apr 8, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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?
     
    PoPSiCLe, Apr 8, 2015 IP
  3. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #3
    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.
     
    Jeremy Benson, Apr 8, 2015 IP
  4. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #4
    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.
     
    Jeremy Benson, Apr 8, 2015 IP
  5. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #5
    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:
     
    Last edited: Apr 8, 2015
    Jeremy Benson, Apr 8, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    Where's the tokenSet coming from? Where do you fetch it when calling the function?
     
    PoPSiCLe, Apr 9, 2015 IP