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
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.
i have an idea about adding viewcount field which start counting views then at the end of week start from 0 again !!
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
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
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: