![]() |
![]() |
![]() |
||||
Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact us. |
![]() ![]() |
|
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed. |
|
Thread Tools |
![]() |
#1 | |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
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
![]() 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:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#2 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Hellllpppp
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#3 |
Confirmed User
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
|
SELECT * FROM `table` ORDER BY `time` DESC,`views` DESC
.. ? |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#4 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
That was their first suggestion, didn't work :/
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..? |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#5 |
Confirmed User
Industry Role:
Join Date: Jul 2008
Location: Los Angeles
Posts: 942
|
ORDER BY date, title DESC
__________________
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#6 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
You mean views DESC? Doesn't work.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#7 | |
Confirmed User
Join Date: Apr 2009
Posts: 1,319
|
That should have worked. Are you sure the 'views' field is set to an integer type?
Quote:
Code:
SELECT *, DATE_FORMAT(`time`, '%M %e %Y') as `formatTime` FROM `table` ORDER BY `formatTime` DESC, `views` DESC |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#8 | |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#9 |
Guest
Posts: n/a
|
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 |
![]() ![]() ![]() ![]() ![]() |
![]() |
#10 | |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Quote:
|
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#11 |
Confirmed User
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
|
In that case, pull them out in order of the dates, and then use PHP or your preferred poison to split them.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#12 |
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
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 ![]()
__________________
Skype variuscr - Email varius AT gmail |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#13 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
I can group them by date no problem, it's the sorting by views within each date that I can't figure out.
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#14 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Looks like that will work! Kick ass. I do have an auto increment setup already too. Trying that out now thanks!
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#15 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
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
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#16 |
see you later, I'm gone
Industry Role:
Join Date: Oct 2002
Posts: 14,078
|
Try using FROM_UNIXTIME() on your timestamp to convert it to a mysql timestamp format.
__________________
All cookies cleared! |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#17 |
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
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 ![]()
__________________
Skype variuscr - Email varius AT gmail |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#18 |
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
should prob be group by date there not time sorry typo ;p
__________________
Skype variuscr - Email varius AT gmail |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#19 |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Bingo! Works like a charm now
![]() Works fine with group by time, if it ain't broke don't fix it lol. 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. |
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#20 | |
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
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.
__________________
Skype variuscr - Email varius AT gmail |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#21 | |
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
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. |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#22 |
Guest
Posts: n/a
|
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. |
![]() ![]() ![]() ![]() ![]() |
![]() |
#23 | |
Confirmed User
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
|
Quote:
![]()
__________________
Skype variuscr - Email varius AT gmail |
|
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#24 |
Guest
Posts: n/a
|
It's cool, I'll message ya some other time, no big hurry. My daughter turns 1 this weekend so I'll be busy away from the boards.
|
![]() ![]() ![]() ![]() ![]() |
![]() |
#25 | ||
Confirmed User
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
|
Quote:
Quote:
|
||
![]() |
![]() ![]() ![]() ![]() ![]() |
![]() |
#26 |
Guest
Posts: n/a
|
Interesting, I was thinking about putting it in an array and parse it that way.
|
![]() ![]() ![]() ![]() ![]() |