Jump to content

Commenting system not working correctly


davidjones1990

Recommended Posts

Hi im pretty new to php but I was following a tutorial for a commenting system and implemented it onto my blog page. Anyway the problem I have is that as you go down the page each blog post displays its own comments  + the comments for the posts above it. I have checked the database and the data seems to link the correct comments to the correct posts so I am confused to why this is happening. I have looked over my code and cant seem to find a problem (probably because I suck!).

 

Here is the code for the blog page...

 

<?php
include_once ("scripts/checkuserlog.php");
include_once ("scripts/connectToMysql.php");
include_once ("functions/agoTime.php");

$myObject = new convertToAgo;
$username = '';
$errorMsg = '';
$successMsg = '';
$date = date("m.d.y");

if (isset($_GET['id'])) {
 $id = preg_replace('#[^0-9]#i', '', $_GET['id']);
} else if (isset($_SESSION['idx'])) {
 $id = $logOptions_id;
} else {
   header("location: index.php");
   exit();
}

$id = preg_replace('#[^0-9]#i', '', $id);
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");

$existCount = mysql_num_rows($sql); 
if ($existCount == 0) {
 header("location: index.php?msg=user_does_not_exist");
     exit();
}

while($row = mysql_fetch_array($sql)){
$username = $row["username"];
}

if(isset($_POST['workoutName'])){
$workoutName = $_POST['workoutName'];
$workoutDescription = $_POST['workoutDescription'];

if ((!$workoutName) || (!$workoutDescription)) { 

	$errorMsg = 'Please fill in both fields';
} else {

$insertBlog = mysql_query("INSERT INTO blog (userid, workoutName, workoutDescription, datetime) VALUES ('$id', '$workoutName', '$workoutDescription', now())") or die(mysql_error());

if ($insertBlog){
        $successMsg = '<span class="errorMsg">Workout blogged successfully</span>';
    } else {
	$errorMsg = '<span class="errorMsg">Problems arose during the information exchange, please try again later.</span>';
    }	
}
}

$getBlog = mysql_query("SELECT * FROM blog WHERE userid='$id' ORDER BY id DESC");
$blogEntries = mysql_num_rows($getBlog);

if($blogEntries < 1){
$blogDisplay = '<span class="blogName">' .$username. ' has not blogged a workout yet</span>';
} else {
while($row = mysql_fetch_array($getBlog)){

$blogEntryId = $row["id"];

$sql_comment = mysql_query("SELECT * FROM blog_comments WHERE post_id='$blogEntryId' ORDER BY id ASC");

$countComment = mysql_num_rows($sql_comment);
if($countComment > 0){
while($row2 = mysql_fetch_array($sql_comment)){
	$comment_user_id = $row2["comment_user_id"];

	$sql_comment_username = mysql_query("SELECT username FROM members WHERE id='$comment_user_id' LIMIT 1");

	while($row3 = mysql_fetch_array($sql_comment_username)){
		$comment_username = $row3["username"];}

	$post_id = $row2["post_id"];
	$commentBody = $row2["comment_body"];
	$commentDate = $row2["comment_date"];

	$displayCommentList .= '<div>' .$comment_username. ' - ' .$commentBody. ' - ' .$commentDate. '</div>';
}

} else {
$displayCommentList = '';
}
$workoutName = $row["workoutName"];
$workoutDescription = $row["workoutDescription"];
$blogDate = $row["datetime"];
$convertedTime = ($myObject -> convert_datetime($blogDate));
$blogDate = ($myObject -> makeAgo($convertedTime));

$blogDisplay .= '<table width="950px"><tr bgcolor="#DBE4FD"><td><span class="blogName">' .$workoutName. '</span> » <span class="blogDate">' .$blogDate. '</span></td></tr>
<tr bgcolor="#F1F4FE"><td><span class="blackText">' .$workoutDescription. '</span></td></tr>
<tr bgcolor="#DBE4FD" valign="top"><td>' .$displayCommentList. '</td></tr>

<div id="new_comment' .$blogEntryId. '" style="display:none"></div>

<tr><td><textarea id="comment' .$blogEntryId. '"></textarea><input type="submit" value="Comment" onclick="javascript:SendComment(\''.$blogEntryId.'\');" /></td></tr>';

if(isset($_SESSION['id'])&&$_SESSION['id']==$id){
	$blogDisplay .= '<tr bgcolor="#DBE4FD"><td><a href="editBlogPost.php?id=' .$blogEntryId. '"><span class="blackText">Edit Post</span></a> &#8211; <span class="blackText">Delete Post</span></td></tr></table><br /><br />';
}
}
}

if(isset($_POST['deleteButton'])){
$deleteBlog = mysql_query("DELETE FROM blog WHERE id='$blogEntryId'");

if($deleteBlog){
	$successMsg = 'Blog entry deleted successfully';
} else {
	$errorMsg = 'Could not process your request, please try again later';
}
}

?>
<?php
if (isset($_SESSION['id'])&&$_SESSION['id']==$id) {
$blogForm = '<table bgcolor="#DBE4FD" width="950px">
    <form action="member_blog.php" method="post" enctype="multipart/form-data">
    <tr>
    <td width="200px"><span class="blackText">Workout Name:</span></td><td width="650px"><input name="workoutName" type="text" id="workoutName" />    <span class="blackText">Date: ' .$date. '</span></td></tr>
    <tr>
    <td><span class="blackText">Workout Description:</span></td><td><textarea name="workoutDescription" cols="75" rows="10" id="workoutDescription" /></textarea></td></tr>
    <tr><td><input name="submitBlog" id="submit" type="submit" value="Blog!" /></td><td><span class="errorMsg">' .$errorMsg. '' .$successMsg. '</span></td>
    </tr>
    </form>
    </table>';
} else {
$blogForm = '';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo ("$username"); ?>'s Blog</title>
<link href="style/layout.css" rel="stylesheet" type="text/css" />
<link href="style/main.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.4.2.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function SendComment(blogEntryId){
var comment_txt = $("#comment"+blogEntryId).val();

if(comment_txt == ""){
alert("Please enter a comment");	
} else {
	$.post("scripts/blogComment.php", {comment: comment_txt, blogId: blogEntryId} ,function(data){
	$("#new_comment"+blogEntryId).html(data);	
	$("#new_comment"+blogEntryId).slideDown(300);
	});
}
}
</script>
</head>

<body>
<div id="container">
<?php include_once ("bannerFiles/bannerTemplate.php"); ?>
    <?php include_once ("bannerFiles/bannerMenu.php"); ?><br />
    <div id="content">
    <span class="profileUsername"><?php echo ("$username"); ?>'s Workout Blog</span><br /><br />
    <?php echo ("$blogDisplay"); ?><br /><br />
<?php echo ("$blogForm"); ?>
    </div><br /><br />
    <?php include_once ("footerFiles/footerTemplate.php"); ?>
</div>
</body>
</html>

 

...and here is the code for the blogComment.php page

 

<?php
session_start();
include_once ("connectToMysql.php");

if(isset($_POST['comment'])){
$comment = $_POST['comment'];
$blogId = $_POST['blogId'];
$comment_user_id = $_SESSION['id'];
$username = $_SESSION['username'];

$sql = mysql_query("INSERT INTO blog_comments (comment_body, post_id, comment_user_id, comment_date) VALUES ('$comment', '$blogId', '$comment_user_id', now())");

echo '<div>' .$username. ' - ' .$comment. ' - Just Now</div>';

} else {

echo "Failure";

}
?>

 

Thanks in advance for any help :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.