![]() |
PHP/MySQL question: how do you sort posts by date AND views?
Ok I asked this at a coding forum this time before coming here but it seems to have left them stumped :Oh crap
I have a table with a 'time' field which is a timestamp, and lets just say 2 other fields 'views' and 'title'. I want to sort the posts into dates, and then sort each dates posts by views, example below: Quote:
|
Hellllpppp
|
SELECT * FROM `table` ORDER BY `time` DESC,`views` DESC
.. ? |
Quote:
Even tried: SELECT * FROM `table`, DATE_FORMAT(`time`, '%M %e %Y') as formatTime ORDER BY `formatTime` DESC,`views` DESC with no luck. The first one didn't sort them by views at all, and the 2nd one had the ones with most views at the top no matter what date. Maybe I could just sort everything by views, and then as it's looping through throw each post into the group which date it belongs to..? |
ORDER BY date, title DESC
|
Quote:
|
Quote:
Quote:
Code:
SELECT *, DATE_FORMAT(`time`, '%M %e %Y') as `formatTime` FROM `table` ORDER BY `formatTime` DESC, `views` DESC Quote:
|
Quote:
|
Interesting, I've seen sites like this and always wondered myself...
What I think he means is split posts by date, then under each date, list all for that date, in order, like on www.leenks.com |
Quote:
|
In that case, pull them out in order of the dates, and then use PHP or your preferred poison to split them.
|
This should help you out :)
mysql> desc test; +-------+--------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+-------------------+-------+ | title | varchar(100) | YES | | NULL | | | views | int(11) | YES | | NULL | | | time | timestamp | YES | | CURRENT_TIMESTAMP | | +-------+--------------+------+-----+-------------------+-------+ mysql> select * from test; +---------------------------------+-------+---------------------+ | title | views | time | +---------------------------------+-------+---------------------+ | news headline | 3300 | 2009-04-30 15:22:57 | | another news headline | 2300 | 2009-04-30 15:23:13 | | another news headline yet again | 5430 | 2009-04-30 15:23:27 | | another news headline yet again | 1060 | 2009-04-29 15:23:48 | | another news headline | 6630 | 2009-04-29 15:24:10 | | news headline | 3200 | 2009-04-29 15:24:25 | +---------------------------------+-------+---------------------+ 6 rows in set (0.00 sec) mysql> select date(time) as date, views, title from test group by time order by date desc, views desc; +------------+-------+---------------------------------+ | date | views | title | +------------+-------+---------------------------------+ | 2009-04-30 | 5430 | another news headline yet again | | 2009-04-30 | 3300 | news headline | | 2009-04-30 | 2300 | another news headline | | 2009-04-29 | 6630 | another news headline | | 2009-04-29 | 3200 | news headline | | 2009-04-29 | 1060 | another news headline yet again | +------------+-------+---------------------------------+ 6 rows in set (0.00 sec) That last one there is what you want. Keep in mind you obviously want to index your fields, and if there are two records with identical timestamps, you either want to group by time,title so they don't get grouped as one or add a primary key (like an auto increment id) to group by time,ID (preventing the rare case you have an entry with exactly the same title and time heh). Enjoy :thumbsup |
Quote:
|
Quote:
|
Shit, it's still placing the highest viewed posts at the very top no matter the date. Does it matter that my time field is the equivalent of time(), a unix timestamp, not necessarily a timestamp like you have..? How come yours works :(
|
Quote:
|
select from_unixtime(time,"%Y-%m-%d") as date, views, title from test group by time order by date desc, views desc;
EDIT: as sarettah said while I was off taking a shower :upsidedow |
Quote:
|
Bingo! Works like a charm now :) - I knew GFY would get the job done, to think I've been slowing posting back and forth for a day or so about this on a coding forum, and found a solution on here within an hour and a half.
Quote:
I think I might use your style timestamp from now on though so I can at least read it easier, how do I insert that type of format when I'm inserting data? I know how to structure the table just not what command to put that format. |
Quote:
Actually myself, I always use unix timestamps, here I just thought when you said timestamp in your first post you meant column type, that's why I did it like that. I'd say there isn't much difference between things though it depends on your app, might save a few function calls here and there if you chose one type over the other. |
Quote:
I was posting at codingforums.com - probably could have found a busier forum but it seemed ok. GFY is just that all in one deal, would prefer to ask here. |
Oh, I thought you ment grouping them together like Leenks does, or Evilhumor.com
Anybody have any idea on how they go about that? Varius mind if I hit you up and pick your brain on some stuff? You seem like a good person to talk to coding wise. |
Quote:
|
Quote:
|
Quote:
Quote:
|
Interesting, I was thinking about putting it in an array and parse it that way.
|
All times are GMT -7. The time now is 12:49 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123