How can I select articles that has top views in a week?

Discussion in 'PHP' started by Hannaspice, Jan 24, 2011.

  1. #1
    Hi there,

    I am here again, 'cause I have a question and hope you will answer me. Please don't let this thread with no comments.

    Table "news":

    -id
    -...
    -views
    -date

    $sql = "SELECT `id`, `views` FROM news ORDER BY `views` DESC LIMIT 0, 20";
    PHP:
    Now my question is how can I select those articles which have top views in a week?
    I am not good at programming date (time) codes, so please help me.
     
    Hannaspice, Jan 24, 2011 IP
  2. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    $sql = "SELECT `id`, `views` FROM news WHERE date >= start_day_of_current_week ORDER BY `views` DESC LIMIT 0, 20";
    
    Code (markup):
    replace start_day_of_current_week with the proper date.

    I hope this one helps.
     
    drctaccess, Jan 24, 2011 IP
  3. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    I would like a code which I dun have to change.
     
    Hannaspice, Jan 25, 2011 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Whats the type for the date column?
     
    danx10, Jan 25, 2011 IP
  5. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #5
    my date column datetime: date(Y-m-d H:i:s)
     
    Hannaspice, Jan 26, 2011 IP
  6. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #6
    
    $now = mktime(0,0,0, date('n'), date('j'), date('Y'));
    $day = date('w');
    $current_week_start = date('Y-m-d H:i:s', $now-$day*86400);
    $sql = "SELECT `id`, `views` FROM news WHERE date >=' . $current_week_start . ' ORDER BY `views` DESC LIMIT 0, 20";
    
    Code (markup):
    The code above will always display top users in current week.

    I hope this helps.
     
    drctaccess, Jan 26, 2011 IP
  7. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #7
    
    $sql = "SELECT `id` FROM news WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY) <= date ORDER BY `views` DESC LIMIT 0, 20";
    
    PHP:
     
    MyVodaFone, Jan 26, 2011 IP
  8. Hannaspice

    Hannaspice Active Member

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #8
    It works great!
    Thank u all.
     
    Hannaspice, Jan 30, 2011 IP