10 most popular videos of the last seven days

Discussion in 'PHP' started by paul174, Aug 24, 2011.

  1. #1
    hello
    please need some help with php ,i am trying to write a script listing the most popular videos viewed the last 7 days..i have in mysql table the fields id, total views ...
    thanks
     
    paul174, Aug 24, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Do you keep track of individual views? There would be no way to use total views assuming it is total views without respect to any period of time.
     
    jestep, Aug 24, 2011 IP
  3. paul174

    paul174 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    no i don't, i think i have to add a field with timestamp but don't know what to do !
     
    paul174, Aug 24, 2011 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,899
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #4
    I think you'll find you need a new table to collect the date and the viewcount.
     
    sarahk, Aug 24, 2011 IP
  5. paul174

    paul174 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #5
    i have an idea about adding viewcount field which start counting views then at the end of week start from 0 again !!
     
    paul174, Aug 25, 2011 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    just create a new table for the 'viewing' stats with only a couple of fields (video_id, date(dd-mm-YYYY), views) and add everytime a user views a video a new row to this table!

    Ps trick to overcome cheaters! (for generating more views) add another table (viewing_ips) and add the following fields (ipaddress, video_id, date(dd-mm-yyyy), views) and before you update the previous table ('viewing') you could insert a row into this with INSERT INTO .... ON DUPLICATE KEY UPDATE that way you can check if the user already viewed this video (mysql_affected_rows()) or not. When the user has not viewed the video insert another view in the 'viewing' table.

    Then add a cronjob for the 'viewing_ips' table to run every day that clears all views from the day before yesterday.

    Questions? my PM is still available ;)

    b.t.w next time add more information to your question, like sample (scripting/programming/html) codes and database/table structure
     
    EricBruggema, Aug 25, 2011 IP
  7. paul174

    paul174 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #7
    thank you Eric ,here is what i did so far, added field named tmpviews return 1 every friday ,
    
    //Give what day of the week it is. then check if it's friday.
    	$day = date("l");
    	
    	if ($day == "Friday"){
    		$query = sprintf("UPDATE videos SET tmpviews=1 WHERE id=%d",tosql($id,"int"));
    		}
    	else {
    		$query = sprintf("UPDATE videos SET tmpviews=tmpviews+1 WHERE id=%d",tosql($id,"int"));
    		}
    
    PHP:
    then
    $query = mysql_query("SELECT id,thumb,title,tmpviews FROM videos WHERE tmpviews!=' ' ORDER BY tmpviews DESC LIMIT 10");
    PHP:
    then display it!
    but for example on friday it will display only the most viewed on friday, and on saturday only display the last two days and so on ! is there any other idea
     
    Last edited: Aug 25, 2011
    paul174, Aug 25, 2011 IP
  8. paul174

    paul174 Greenhorn

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    just had to correct a mistake and change this line of code to return the fields tmpviews all at once,
    from this
    
            $query = sprintf("UPDATE videos SET tmpviews=1 WHERE id=%d",tosql($id,"int"));
            
    PHP:
    to this
    
            $query = sprintf("UPDATE videos SET tmpviews=1 ");   
    
    PHP:
     
    paul174, Aug 26, 2011 IP