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.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 04-30-2009, 04:06 PM   #1
Jakez
Confirmed User
 
Jakez's Avatar
 
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:
April 29th
5430 views - News headline
3300 views - Another News headline
2300 views - Yet Another News headline

April 28th
6630 views - News headline
3200 views - Another News headline
1060 views - Yet Another News headline
I'm sure it can't be that hard.. maybe a simple query and some arrays idk.
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-30-2009 at 04:08 PM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:27 PM   #2
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Hellllpppp
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:30 PM   #3
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
SELECT * FROM `table` ORDER BY `time` DESC,`views` DESC
.. ?
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:37 PM   #4
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by quantum-x View Post
SELECT * FROM `table` ORDER BY `time` DESC,`views` DESC
.. ?
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..?
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-30-2009 at 04:41 PM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:42 PM   #5
KickAssJesse
Confirmed User
 
KickAssJesse's Avatar
 
Industry Role:
Join Date: Jul 2008
Location: Los Angeles
Posts: 942
ORDER BY date, title DESC
__________________

Contact - email: jesse~AT~atkcash~DOT~com - Skype: kickassjesse - ICQ: 386185547
ATK Cash $$$
KickAssJesse is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:45 PM   #6
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by KickAssJesse View Post
ORDER BY date, title DESC
You mean views DESC? Doesn't work.
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 04:51 PM   #7
ProG
Confirmed User
 
Join Date: Apr 2009
Posts: 1,319
Quote:
Originally Posted by Jakez View Post
That was their first suggestion, didn't work :/
That should have worked. Are you sure the 'views' field is set to an integer type?

Quote:
Originally Posted by Jakez View Post
Even tried: SELECT * FROM `table`, DATE_FORMAT(`time`, '%M %e %Y') as formatTime ORDER BY `formatTime` DESC,`views` DESC
This query looks invalid. It should be something more like this:

Code:
SELECT *, DATE_FORMAT(`time`, '%M %e %Y') as `formatTime` FROM `table` ORDER BY `formatTime` DESC, `views` DESC
Quote:
Originally Posted by Jakez View Post
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..?
I think ultimately you will want to create an array for ease of outputting the data. If you only sort by views, then build the array, you will still need to sort by date (which is much easier to do with mysql).
ProG is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:00 PM   #8
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by ProG View Post
That should have worked. Are you sure the 'views' field is set to an integer type?



This query looks invalid. It should be something more like this:

Code:
SELECT *, DATE_FORMAT(`time`, '%M %e %Y') as `formatTime` FROM `table` ORDER BY `formatTime` DESC, `views` DESC


I think ultimately you will want to create an array for ease of outputting the data. If you only sort by views, then build the array, you will still need to sort by date (which is much easier to do with mysql).
views field is an integer, both of those queries output the same thing, they're printing the posts with the highest views at the top of everything, and the rest is sorted by date below them, but only because 98% of the data has 0 views because this is just in the testing stages and there are only like 2-3 posts with views over 0. Otherwise it would all probably just be sorted by views and the dates would be all mixed up.
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-30-2009 at 05:01 PM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:09 PM   #9
Killswitch - BANNED FOR LIFE
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
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:12 PM   #10
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by Killswitch View Post
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
Well leenks doesn't seem to be sorting them by the views or anything. My first post pretty much explains how I want it, everything is grouped by their date and within each date the posts are sorted by views..
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:16 PM   #11
quantum-x
Confirmed User
 
quantum-x's Avatar
 
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.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:18 PM   #12
Varius
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
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:18 PM   #13
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by quantum-x View Post
In that case, pull them out in order of the dates, and then use PHP or your preferred poison to split them.
I can group them by date no problem, it's the sorting by views within each date that I can't figure out.
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:20 PM   #14
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by Varius View Post
Enjoy
Looks like that will work! Kick ass. I do have an auto increment setup already too. Trying that out now thanks!
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:25 PM   #15
Jakez
Confirmed User
 
Jakez's Avatar
 
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
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-30-2009 at 05:28 PM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:32 PM   #16
sarettah
see you later, I'm gone
 
Industry Role:
Join Date: Oct 2002
Posts: 14,078
Quote:
Originally Posted by Jakez View Post
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
Try using FROM_UNIXTIME() on your timestamp to convert it to a mysql timestamp format.
__________________
All cookies cleared!
sarettah is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:37 PM   #17
Varius
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
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:40 PM   #18
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by Varius View Post
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
should prob be group by date there not time sorry typo ;p
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:44 PM   #19
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
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:
Originally Posted by Varius View Post
should prob be group by date there not time sorry typo ;p
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.
__________________
[email protected] - jakezdumb - 573689400

Killuminati

Last edited by Jakez; 04-30-2009 at 05:48 PM..
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:54 PM   #20
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by Jakez View Post
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.



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.
I think you need a better coding forum to post at

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
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 05:57 PM   #21
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by Varius View Post
I think you need a better coding forum to post at

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.
Ah true, I do use the numbering of the unix timestamp in a lot of ways so I might not want to change it.

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.
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 06:20 PM   #22
Killswitch - BANNED FOR LIFE
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.
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 07:30 PM   #23
Varius
Confirmed User
 
Industry Role:
Join Date: Jun 2004
Location: New York, NY
Posts: 6,890
Quote:
Originally Posted by Killswitch View Post
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.
Sure, got some stuff to do to get ready for the beach leaving at 3am and will be back prob Sunday night, but might be online a bit later tonight (as I doubt I'll sleep) so feel free to ICQ 520468 or Skype variuscr me
__________________
Skype variuscr - Email varius AT gmail
Varius is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 04-30-2009, 10:15 PM   #24
Killswitch - BANNED FOR LIFE
Guest
 
Posts: n/a
Quote:
Originally Posted by Varius View Post
Sure, got some stuff to do to get ready for the beach leaving at 3am and will be back prob Sunday night, but might be online a bit later tonight (as I doubt I'll sleep) so feel free to ICQ 520468 or Skype variuscr me
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.
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-01-2009, 06:51 PM   #25
Jakez
Confirmed User
 
Jakez's Avatar
 
Industry Role:
Join Date: Jan 2004
Location: oddfuturewolfgangkillthemall!!!!!!!
Posts: 5,656
Quote:
Originally Posted by Killswitch View Post
Oh, I thought you ment grouping them together like Leenks does, or Evilhumor.com

Anybody have any idea on how they go about that?
Here's how I did it basically, removed all the html to make it look more simple but it should still print out fine:

Quote:
<?php
$result=mysql_query("SELECT *, from_unixtime(time,'%Y-%m-%d') as date FROM songs GROUP BY time ORDER BY date DESC, views DESC");
while($row=mysql_fetch_array($result))
{
if(!$date)
{
$date=date("l F j",$row[time]);
?>
<b><?=$date?></b><br>
<?=$row[artists]?> - <?=$row[title]?><br>
<?php
}
elseif(date("l F j",$row[time])==$date)
{
?>
<?=$row[artists]?> - <?=$row[title]?><br>
<?php
}
elseif(date("l F j",$row[time])!=$date)
{
$date=date("l F j",$row[time]);
?>
<b><?=$date?></b><br>
<?=$row[artists]?> - <?=$row[title]?><br>
<?php
}
}
?>
__________________
[email protected] - jakezdumb - 573689400

Killuminati
Jakez is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 05-01-2009, 06:55 PM   #26
Killswitch - BANNED FOR LIFE
Guest
 
Posts: n/a
Interesting, I was thinking about putting it in an array and parse it that way.
  Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.