main concept - videos being watched now

Discussion in 'PHP' started by dracula51, Oct 28, 2009.

  1. #1
    can anyone giv the main concept of "videos being watched now"
    u see that on youtube or some other video sharing site.

    how to make such thing that can track which videos r being watched & return those ???
     
    dracula51, Oct 28, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Simple concept:
    When someone starts watching video insert a record in database with current time.
    So videos watched now are for example 5 records sortred by date desc.
    Clean this database for records older than some given time.
    More details - make a record through ajax request some time after page load so make sure a person is wathching video and didn't go to another page.
     
    AsHinE, Oct 28, 2009 IP
  3. sunchiqua

    sunchiqua Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    view.php?id=12
    Code (markup):
    $id = $_GET['id'];
    $timestamp = time();
    
    $reg = mysql_query("INSERT INTO activity (video_id, timestamp) VALUES ('$id', '$timestamp')") or die(mysql_error());
    PHP:
    Make another query to fetch data about videos which timestamp field is bigger than the current time minus 3 minutes ( just an example ) and print them out.
     
    sunchiqua, Oct 28, 2009 IP
  4. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4

    ok i now understand the first step (insert)
    in view.php file i'll add that code..so whenevr someone open it, it will write to DB
    but how to clean it. i mean clean that DB table, which tables r older than 5 minutes
     
    dracula51, Oct 28, 2009 IP
  5. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no he means just clear the table of any records that are for example older than 5 minutes obviously you can tell the record's age by the timestamp that you have recorded it with
     
    adwebtiser, Oct 28, 2009 IP
  6. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    example plz :eek:
     
    dracula51, Oct 28, 2009 IP
  7. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #7
    Well, using database structure by sunchiqua
    
    $timestamp = time()-5*60;
    
    $reg = mysql_query("DELETE FROM activity WHERE timestamp < '$timestamp')") or die(mysql_error());
    
    PHP:
     
    AsHinE, Oct 28, 2009 IP
  8. sunchiqua

    sunchiqua Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You may not even want to delete it so quickly.

    $timelimit = time() - 180;
    $get = mysql_query("SELECT * FROM activity WHERE timestamp < '$timelimit' ORDER BY timestamp DESC LIMIT 0, 15") or die(mysql_error());
    PHP:
    Once in a day or so, let the cron flush the table ( partially .. let's say, everything that's older than 30 minutes ).
     
    sunchiqua, Oct 29, 2009 IP
  9. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #9
    I guess when selecting you should use "greater than" condition, cause if you use "less than" you'll get rows older than given timestamp.
     
    AsHinE, Oct 29, 2009 IP
  10. sunchiqua

    sunchiqua Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Oh, crap :) Thanks for pointing that out.
     
    sunchiqua, Oct 30, 2009 IP