Problem with the blog tutorial...
Posted: Fri Jun 15, 2012 8:32 pm
So been nudging my way through the tutorial and minus the few typo's that I had to go back and fix everything "seems" to be working fine. EXCEPT the get_posts function seems to not work. I am not getting any errors when loading the page, but I am also not getting anything being displayed. I ran the code through myphpadmin came up fine, I did put in a test post but when I ran the code it came up with 0-0 rows which doesnt seem right but i am pretty sure I have the database setup right.
My orignal database name instead of blog is newssys but I don't see how that matters I have the comments and posts tables and nothing is wrong with them.
posts has
post_id int(6) AI Primary
Post_title varchar255
post_body text
post_user varchar 32
post_date datetime
even changed the collaition to latin_swedish_ci for the ones that had it to match teh tutorials. nothing. I can verify that the test post is in there as well.
here is the function and the code from blog_list.php
[syntax=php]
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;
}
[/syntax]
[syntax=php]<div>
<?php
$posts = get_posts();
foreach ($posts as $post){
?>
<h2><?php $post['title']; ?></h2>
<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
}
?>
</div>[/syntax]
My orignal database name instead of blog is newssys but I don't see how that matters I have the comments and posts tables and nothing is wrong with them.
posts has
post_id int(6) AI Primary
Post_title varchar255
post_body text
post_user varchar 32
post_date datetime
even changed the collaition to latin_swedish_ci for the ones that had it to match teh tutorials. nothing. I can verify that the test post is in there as well.
here is the function and the code from blog_list.php
[syntax=php]
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;
}
[/syntax]
[syntax=php]<div>
<?php
$posts = get_posts();
foreach ($posts as $post){
?>
<h2><?php $post['title']; ?></h2>
<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
}
?>
</div>[/syntax]