tags:

views:

151

answers:

2

I have a social network so a blog is only you could say a widget of the site for a user, so every user can have a blog, I would like to make this blog as nice as possible. WHen you go to a users blog page it shows all blogs for the current month.

My question, how can I show a column on the side like this July 2009 June 2009 May 2009 April 2009 March 2009 February 2009

The catch is it should only list months since the user joined the site until current, so a user that joined2 years ago from now would have 24 of these links and a user just 1 month or newer would only see 1?

The system uses php/mysql

+1  A: 

This depends on how your database is structured. Generally, a blog post has a date associated with it. You can usually query your database to get a list of unique months for each user. Are you using an OSS system like Wordpress, or something custom? Can you give any insight on your schema?

Toby Joe Boudreaux
I don't use any premade software, its just a blog mysql table and there is a date posted column as well as a userID posted so I currently just query for all blogs from that users but I want to show a column on the right side with links to view by month I just though about it the easiest way would probably be some math to determine how many months they have been there from the signup date which is in a session variable
jasondavis
+2  A: 

Hi,

Assuming you :

  • only want to take into account months when there is a blog-post from the user
  • your DB is MySQL

You could try something like this :

select concat(YEAR(date), '-', MONTH(date)) as month, count(*) as num 
from ab_post 
where user_id = 1 
group by YEAR(date), MONTH(date) 
order by YEAR(date) desc, MONTH(date) desc;

It will :

  • list the years/month when there has been a post from user "1" (this has to be dynamic, of course)
  • for each month, give how many posts there are

Which means you'll get something like this :

+--------+-----+
| month  | num |
+--------+-----+
| 2008-6 |   1 |
| 2008-5 |   1 |
| 2008-4 |   3 |
| 2008-3 |   1 |
+--------+-----+

Thing to notice :

  • grouping by year/month
  • ordering by date desc

As a side note, this will give month as 1 digit for january to september : up to you to find the right format in MySQL's help ;-)

Have fun !

Pascal MARTIN
Thanks that is probablt the best way to do it but for performance every query counts and I think this would be an extra query, I store the users signup date, could I do some math to just determine the months they have been a member, even if a month has 0 post I will show it
jasondavis
Like I take the users signup date and todays date and retrun an array of months in between that somehow
jasondavis
If you don't care about displaying months with no post, your solution is probably the best, yes :-) (but, as a end-user, I would think it's unusual -- especially if the months are links to pages which display posts of that month, which means pages with no post) ; another solution, to not make too many requests would be to use some kind of caching mecanism for the "block" which contains those links (obviously, means there would be one cache per user, and it has to be deleted when a post is create/deleted/published/unpublished/...)
Pascal MARTIN
Yes thanks for the tips, so much work for such a little feature
jasondavis
you're welcome :-) (and, yes, always so much troubles for little things ^^ )
Pascal MARTIN