Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    63
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Comment system display not working

    Hey, I have just finished a really simple PHP wall and commenting system. It works fine but I am having trouble displaying the comments correctly. The comments seem to display their own comments plus the comments on posts above it. I have looked in the database and the comments correspond to the correct wall posts so I am confused to why this is happening. For example;

    POST 1
    -Comment id=1
    POST 2
    -Comment id=1
    -Comment id=2

    but it should be:

    POST 1
    -Comment id=1
    POST 2
    -Comment id=2

    here is the code

    Code:
    <?php
    $wallDisplay = '';
    $commentDisplay = '';
    
    $wallDisplaySql = mysql_query("SELECT * FROM wall WHERE to_id='$id' ORDER BY datetime DESC") or die (mysql_error());
    
    while($row = mysql_fetch_array($wallDisplaySql)){
    	$wallPostId = $row["id"];
    	$to_id = $row["to_id"];
    	$from_id = $row["from_id"];
    	$message = $row["message"];
    	$dateTime = $row["datetime"];
    
    	
    	$getFromData = mysql_query("SELECT username FROM members WHERE id='$from_id'") or die (mysql_error());
    	while($row2 = mysql_fetch_array($getFromData)){$wallUsername = $row2['username'];}
    	
    	$displayComments = mysql_query("SELECT * FROM wallComments WHERE wallPostId='$wallPostId' ORDER BY datetime ASC");
    	$countComments = mysql_num_rows($displayComments);
    	if($countComments > 0){
    
    	while($row3 = mysql_fetch_array($displayComments)){
    		$commentId = $row3['id'];
    		$wallComment = $row3['comment'];
    		$commentFrom = $row3['from_id'];
    		$commentDate = $row3['datetime'];
    	
    	
    	$getUsername = mysql_query("SELECT username FROM members WHERE id='$commentFrom'");
    	
    	while($row4 = mysql_fetch_array($getUsername)){$commentUsername = $row4['username'];}
    	
    	$cheersCheck_pic = "members/$commentFrom/pic1.jpg";
    	$cheersDefault_pic = "members/0/defaultMemberPic.jpg";
    	if (file_exists($cheersCheck_pic)) {
        $cheers_pic = "<img src=\"$cheersCheck_pic?$cacheBuster\" width=\"40px\" />"; 
    	} else {
    	$cheers_pic = "<img src=\"$cheersDefault_pic\" width=\"40px\" />"; 
    	}
    
    	$commentDisplay .= '<table width="500px" align="right" cellpadding="4" bgcolor="#FFF">
            <tr>
              <td width="10%" bgcolor="#FFFFFF"><a href="member_profile.php?id=' . $commentFrom . '">' . $cheers_pic . '</a><br />
              </td>
              <td width="90%" bgcolor="#DBE4FD"><a href="member_profile.php?id=' . $commentFrom . '"><span class="blackText">' . $commentUsername . '</span></a> &bull; <span class="blackTetx">' . $commentDate . '<br /><font size="1"></font></span><br />
              <span class="blackText">' . $wallComment . '</span></td>
            </tr>
          </table>';
    	
    		}
    	} else {
    		$commentDisplay = '';
    	}
    	
    	$cheersCheck_pic = "members/$from_id/pic1.jpg";
    	$cheersDefault_pic = "members/0/defaultMemberPic.jpg";
    	if (file_exists($cheersCheck_pic)) {
        $cheers_pic = "<img src=\"$cheersCheck_pic?$cacheBuster\" width=\"40px\" />"; 
    	} else {
    	$cheers_pic = "<img src=\"$cheersDefault_pic\" width=\"40px\" />"; 
    	}
    	
    	$wallDisplay .= '<table width="100%" align="center" cellpadding="4" bgcolor="#FFF">
            <tr>
              <td width="7%" bgcolor="#FFFFFF"><a href="member_profile.php?id=' . $from_id . '">' . $cheers_pic . '</a><br />
              </td>
              <td width="93%" bgcolor="#DBE4FD"><a href="member_profile.php?id=' . $from_id . '"><span class="blackText">' . $wallUsername . '</span></a> &bull; <span class="blackTetx">' . $dateTime . '<br /><font size="1"></font></span><br />
              <span class="blackText">' . $message . '</span></td>
            </tr>
          </table>
    	  <div>' . $commentDisplay . '</div>
    	  <div id="comment" align="right">
    		<form id="comment" name="comment" method="post" action="member_profile.php?id=' .$id. '">
    		<textarea name="comment" id="comment" rows="1" cols="35"></textarea>
    			<input type="hidden" name="wallPostId" id="wallPostId" value="'. $wallPostId .'" />
    			<input type="hidden" name="commentFrom" id="commentFrom" value="'. $_SESSION['id'] .'" />
    		<input type="submit" name="submitComment" id="submitComment" />
    		</form>
    		</div><br />';
    }
    ?>
    Thanks in advance for any help
    Last edited by davidjones1990; 09-10-2011 at 06:27 PM. Reason: I moved some code around


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •