Page 1 of 1

blog_list issue

Posted: Thu Dec 08, 2011 10:52 pm
by guitardude211
Hello!

When I watched the tutorial on how to create a blog, on the part 4 video, I am having an issue with the content showing up on the "blog_list.php". For some reason nothing shows on that page where the "get_posts" function should output what is in the database as well as the html formating. I have a feeling I did something wrong in the "get_posts" function but then again, It could just be that I did something wrong in the "blog_list" Any help would be greatly appreciated :)

here are the two files in code:

blog_list.php
<?php

include('core/init.inc.php');

?>
<!DOCTYPE html PUBLIC "-//WC3//DTD XHTML 1.0 Strict//EN" "http://www.w3/org/TR/xhtml1/DTD/xhtml-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Blog</title>
	</head>
	<body>
		<div>
			<?php
			
			$posts = get_posts();
			
			foreach($posts as $post){
				?>
				<h2><?php echo $post['title']; ?></h2>
				<h4>By <?php echo $post['user']; ?> on <?php $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
			}
			
			?>
		</div>
	</body>
</html>
and here is "posts.inc.php"
<?php

//checks if the given post id is in the table
function valid_pid($pid) {
	$pid = (int)$pid;
	
	$total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = {$pid}");
	$total = mysql_result($total, 0);
	
	if ($total != 1) {
		return false;
	}else{
		return true;
	}
}

//gets a summary of all blog posts
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) {
		$row[] = 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;
}

//gets a single post from the table
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;
}

//adds a new blog entry
function add_post($name, $title, $body) {
	$name = mysql_real_escape_string(htmlentites($name));
	$title = mysql_real_escape_string(htmlentites($title));
	$body = mysql_real_escape_string(nl2br(htmlentites($body)));
	
	mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())");
}

?>

Re: blog_list issue

Posted: Thu Dec 08, 2011 11:54 pm
by Temor
you never populate the $rows array, so the function is returning an empty array.
not sure if that is a typo or not.

Re: blog_list issue

Posted: Fri Dec 09, 2011 12:11 am
by jacek
Already answered here http://betterphp.co.uk/board/viewtopic. ... 6987#p6998

Please do not double post.