problems with blog

Post here is you are having problems with any of the tutorials.
Post Reply
joc
Posts: 5
Joined: Sat Oct 08, 2011 1:03 am

problems with blog

Post by joc »

hi.. im using your blog script and im having problem with the commenting system .. the comments are not showing under the blog entry even though it is being sent to the database can you help me ? .. here are some of my codes ..
<?php
include('init.inc.php');

if(isset($_GET['pid'], $_POST['user'] , $_POST['body'])){
	if(add_comment($_GET['pid'] , $_POST['user'] , $_POST['body'])){
		header("Location: blog_read.php?pid={$_GET['pid']}");
	}else{
		header("Location: blog_list.php");
	}

	die();
}

?>


<!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" dir="ltr" lang="en-US" xml:lang="en">
<head>
<body>
<?php
					if(isset($_GET['pid']) == false || valid_pid($_GET['pid']) == false)					{
						echo "Invalid post ID.";
					}else{
						$post = get_post($_GET['pid']);
					
					?>	
					
					<h2><?php echo $post['title']; ?></h2>
					<h4>By <?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4>
					
					<hr>
					<p><?php echo $post['body']; ?></p>
					<hr>
					
					<?php
					foreach($post['comments'] as $comment){
					?>
					<h4>By <?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4>
					<p><?php echo $comment['body']; ?></p>
				<?php
				}
				?>
					
					
					
					<h3>Comment : </h3>
					<form action="" method="post">
						<p>
						
							
						Title :	<input type="text" name="user" id="user">
						</p>
						<p>
							<textarea name="body" rows="20" cols="60"></textarea>
						</p>
						<p>
							<input type="submit" value="Add Comment">
						</p>
					</form>
				<?php
				}
				?>		


Last edited by jacek on Sat Oct 08, 2011 9:32 pm, edited 1 time in total.
Reason: code tags...
joc
Posts: 5
Joined: Sat Oct 08, 2011 1:03 am

Re: problems with blog

Post by joc »

im also getting double posts in the blog_list.php everytime i post from the form .. do you know what is the cause of this problem ?? .. thanks ..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: problems with blog

Post by jacek »

What does the code of you functions look like ?
Image
joc
Posts: 5
Joined: Sat Oct 08, 2011 1:03 am

Re: problems with blog

Post by joc »

my comment. inc

<?php

function get_comments($pid){ 
	$pid = (int)$pid;
	
	$sql =	"SELECT
			comment_body AS 'body',
			comment_user AS 'user',
			DATE_FORMAT(comment_date, '%d/%m/%Y %H:%i:%s') AS 'date'
		 FROM comments
		 WHERE post_id = {$pid}";
		 
	$comments = mysql_query($sql);
	
	$return = array();
	while (($row = mysql_fetch_assoc($comments)) != false){
		$return = $row;
	}

	return $return;

}

function add_comment($pid, $user , $body){
	if(valid_pid($pid) == false){
		return false;
	}

	$pid	= (int)$pid;
	$user	= mysql_real_escape_string(htmlentities($user));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO comments (post_id, comment_user , comment_body , comment_date) VALUES ('{$pid}' , '{$user}' , '{$body}' , NOW())");

	return true;
}

?>

posts.inc
<?php

function valid_pid($pid){
	$pid = (int)$pid;
	
	$total = mysql_query("SELECT COUNT(post_id) FROM posts WHERE post_id = {$pid}")
	or die(mysql_error());
	$total = mysql_result($total , 0);
	
	if($total != 0){
		return false;
	}else{
		return true;
	}
}



function get_posts(){
	$sql = "SELECT
				posts . post_id AS 'id',
				posts . post_title AS 'title',
				LEFT(posts . post_body , '512') AS 'preview',
				posts . post_user AS 'user',
				DATE_FORMAT(posts . post_date , '%d/%m/%Y %H:%i:%s') AS 'date',
				comments . total_comments,
				DATE_FORMAT(comments . last_comment , '%d/%m/%Y %H:%i:%s') AS 'last_comment'
			FROM posts
			LEFT JOIN (
				SELECT
					post_id,
					COUNT('comment_id') AS 'total_comments',
					MAX('comment_date') AS 'last_comment'
				FROM comments
				GROUP BY post_id
			) AS comments
			ON posts . post_id = comments . post_id
			ORDER BY posts . post_date DESC";
		
		
			
	$posts = mysql_query($sql);
	
	$rows = array();		
	while(($row = mysql_fetch_assoc($posts)) != false){
		$rows[] = array(
			'id'				=> $row['id'],
			'title'				=> $row['title'],
			'preview'			=> $row['preview'],
			'user'				=> $row['user'],
			'date'				=> $row['date'],
			'total_comments'	=> ($row['total_comments'] == null) ? 0 : $row['total_comments'],
			'last_comment'		=> ($row['last_comment'] == null) ? 'never' : $row['last comment']
		);
	}	
	return $rows;
}


function get_post($pid){
	$pid = (int)$pid;
	
	$sql = "SELECT
				post_title AS 'title',
				post_body AS 'body',
				post_user AS 'user',
				post_date AS 'date'
			FROM  posts
			WHERE post_id = {$pid}";
			
	$post = mysql_query($sql);
	
	$post = mysql_fetch_assoc($post);
	
	$post['comments'] = get_comments($pid);
	
	return $post;
	
}



function add_post($name , $title , $body){
	$name	= mysql_real_escape_string(htmlentities($name));
	$title	= mysql_real_escape_string(htmlentities($title));
	$body	= mysql_real_escape_string(nl2br(htmlentities($body)));
	
	mysql_query("INSERT INTO posts (post_user , post_title , post_body , post_date) VALUES ('{$name}' , '{$title}' , '{$body}' , NOW())")
	or die(mysql_error());


}
?>
for the posts .. it is being sent to the database as a single entry but echoing in the page in multiple entry ..
for the comments .. it is also being sent to the database but not being echoed below the blog entry ..

i hope you can help me in this matter .. thanks .
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: problems with blog

Post by jacek »

$return = $row;
Here you are setting $return to the value of $row, what you should be doing is adding $row to the end of the array.
$return[] = $row;
Can you also post the code of the main page ? If that is where the posts are being displayed twice ?
Image
joc
Posts: 5
Joined: Sat Oct 08, 2011 1:03 am

Re: problems with blog

Post by joc »

so you mean i should make $row++ after the $result = $row ? ..
could you be more specific please .. i'm new to these stuffs .. im sorry ..

here's the page where the posts are being doubled ..

<?php
include('init.inc.php');

?>


<html>
<head></head>
<body>
<?php
						
						$posts = get_posts();
						
						foreach($posts as $post){
						?>
						
						<h2><a href="blog_read.php?pid=<?php echo $post['id']; ?>"><h2><?php echo $post['title']; ?></h2></a>
						<h4>By <?php echo $post['user']; ?> on <?php echo $post['date'];?></h4>
						<h4>(<?php echo $post['total_comments']; ?> comments), last comment <?php echo $post['last_comment']; ?></h4>
						
						<hr>					
					
					
						<p><?php echo $post['preview']; ?></p>
					<?php
					}
					?>	
</body>
</html>
in case you also need the code for the post form ..
<?php
include('init.inc.php');

if(isset($_POST['user'], $_POST['title'] , $_POST['body'])){
	add_post($_POST['user'] , $_POST['title'] , $_POST['body']);
	header("Location:blog_list.php");
	die();
 
}

?>
<html>
<head></head>
<body>
	<form action="" method="post">
						<p>
							<label for="user">Name</label>
							<input type="text" name="user" id="user">
						</p>
						<p>
							<label for="title">Title</label>
							<input type="text" name="title" id="title">
						</p>
						<p>
							<textarea name="body" rows="20" cols="60"></textarea>
						</p>
						<p>
							<input type="submit" value="Add Post">
						</p>
					</form>
</body>
</html>

User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: problems with blog

Post by jacek »

joc wrote:so you mean i should make $row++ after the $result = $row ? ..
No, the second code block in my above post is what you need to do. Notice the [].

What exactly do you mean by doubled ? Each post is shown twice on the main page ? I don't see anything that would cause that to happen :s
Image
joc
Posts: 5
Joined: Sat Oct 08, 2011 1:03 am

Re: problems with blog

Post by joc »

oh forget about the second problem .. it seems that the code the causes it is the code that i'm trying which is limiting the blog posts on the page by 10 ..

you have any idea on how can i do it ? .. i mean .. i want to show only 10 blog entries on the page then i must have the next button to show the next 10 blog entries .. coz i have a lot of blog entries ..

by the way .. thanks for the wonderful tutorial that you've made .. and also thanks for your time trying to help me ..
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: problems with blog

Post by jacek »

You need to use the mysql LIMIT function to limit the number of rows you get from the table, I think I did a video on it ?
Image
Post Reply