Let me start out by drawing your attention to the left. Strange, yet fascinating little image, huh? I always try to include some kind of interesting ey candy with each blog and depending on my mood, some are better than others. Actually, the real reason I do it is to make sure I didn't break any of the upload functionality with the latest update, but shhhh, don't tell anyone! In this case, that cartoon character's facial expressions were almost identical to mine as I struggled to figure out how to make this 'Popular' section work as planned.

When I first constructed the database tables for the blogs, I made an error in the design. I had initially created a column called views thinking that I could use that column to decipher the most popular blogs for the month, year, and of all time. Umm, does anyone think there is something wrong with that logic? Well, duh!

First of all, a views column won't get me much of anything other than a grand total. How the heck am I supposed to know the blogs which were most popular for the month and year? To combat this mind-boggling dilemma I ended up removing the views column. Instead I created a new table called blog_statistics.

CREATE TABLE `blog_statistics`
    (`id` int(11) NOT NULL auto_increment,
     `blog_id` int(11) NOT NULL default '0',
     `year` int(4) default NULL,
     `month` int(2) default NULL,
     `y_views` int(11) default 0,
     `m_views` int(11) default 0,
     `total` int(11) default 0,
     `created_at` datetime default NULL,
     `updated_at` datetime default NULL,
     foreign key (blog_id) references blogs(id),
     PRIMARY KEY (`id`) )

It may have taken a heart pounding episode of Grey's Anatomy to come to this solution, but here's how it works my friends, at least so far. When you view a blog, those fields are updated like this:

  • If when you view the blog, it's the same year as what's in the table for that record, then it adds to the total year count.
  • If it's the same year and same month, it will add to the year count and month count.
  • If it's the same year, but a different month, it will add to the year count, but reset the month count and set the month field to the current month.
  • If it's a different year, it will reset the year and month count to 1 and set the year and month field to the current year and month respectively.

Got all that? Yeah, me neither, but based on my testing it seems to work. So feel free to check out the Popular Blogs page when you have a chance. It contains the most popular blogs for the month, year, and of all time. If you notice that's it's not functioning as described, get in touch with me.

Next up, delete images via AJAX calls.

Current Rating: 2.0 rating from 1 vote

  • Current rating is 2
  •  
  •  
  •  
  •  
  •  

One Response to "Our Most Popular Blogs"

263147_thumb

Damon

February 13, 2006 at 12:37 am

I almost forgot, the comments section should now work as designed including the ability to “Remember my info”, which I must say was quite a hassle. Since it’s 12:36AM my time, I decided to just add a comment here a) to make sure it’s working and b) because I’m too tired to write another blog tonight.

Comments are Closed

Name: (Required)
Website:
Comment:
Remember my info