Blog Archive by Month, Year

Post here is you are having problems with any of the tutorials.
Post Reply
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Blog Archive by Month, Year

Post by Temor »

I really haven't done anything with that blog code you're referring to, but I know Jacek made a Database Searching tutorial using that blog as a base.

http://betterphp.co.uk/playlist.html?pi ... 759BABE5EC

I haven't watched it in ages so I can't remember if it does exactly what you want, but it should get you on the way. I'll be available to help if you have any questions.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Blog Archive by Month, Year

Post by Temor »

There are a few things I would do differently, and some I wouldn't do at all.
First of all, I would just fetch the post_date as is. It is a unix timestamp if I'm not mistaken, and those are really easy to manipulate with PHP instead of SQL.

[syntax=php]
function get_archive(){
$sql = "SELECT
`post_date` AS `date`
COUNT(*) AS `total`
FROM `posts`
$result = mysqli_query($sql);

$rows = array();
while (($row = mysqli_fetch_assoc($result)) != false){
$rows[] = array(
'date' => $row['date'],
'count' => $row['total']
);
}

return $rows;
}[/syntax]

I would then use php functions to make the timestamp readable in the way you want.
Check out the date() function.

My head isn't on straight today. I've been without sleep for 48+ hours so I might not be thinking right.
Let me know if it's not working and I'll come back when I've slept some.
Post Reply