Morthian Posted April 17, 2007 Share Posted April 17, 2007 I want to have a section on my site that shows the most popular content. I see this on a lot of sites, but I have no idea how to do it myself. Could anyone help me out with this? Quote Link to comment Share on other sites More sharing options...
Morthian Posted April 17, 2007 Author Share Posted April 17, 2007 By the way, just if I wasn't clear, when I say content I mean records in a database. For example, Newgrounds shows its most popular flash submissions on its Portal page. DeviantArt does the same sort of thing with art submissions. I think somehow sorting records by the most views in the last week might be a good way of showing the "most popular" items. But like I said, I would have no clue how to actually do this. Quote Link to comment Share on other sites More sharing options...
chriscloyd Posted April 17, 2007 Share Posted April 17, 2007 Well content im taking it means news. In the news table you need to make a column called views. Then when someone clicks the link to view the news u need to have a script at the top of that page to update the views for that news. So day you had a link like www.mysite.com/viewnews.php?id=4 then at the top of the page you would do <?php $get_news_views = mysql_query("SELECT * FROM `table_name` WHERE news_id = '".$_GET['id']."'") or die(mysql_error()); $news_views = mysql_fetch_array($get_news_views); $views = $news_views['views'] + 1; mysql_query("UPDATE `table_name` SET views = '".$views."' WHERE news_id = '".$_GET['id']."'") or die(mysql_error()); ?> Quote Link to comment Share on other sites More sharing options...
Morthian Posted April 17, 2007 Author Share Posted April 17, 2007 Yes, I know how to record a total number of views. I am asking how to record the number of views in a given time, such as a day or a week. On GameSpot, the popularity of the games is based on total unique views from yesterday. Since my site is certainly not as well known as GameSpot, I figure it would be better to base the popularity of content on views from the last week, or last 7 days. How can I do this? Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 17, 2007 Share Posted April 17, 2007 You could make a new table in your DB called "views", have track the following fields: content_id (int) (unique) views (int) date (date) When the page is accessed, check if a DB row already exists for that content that day and update it. Otherwise create a new row. SELECT COUNT(*) FROM views WHERE content_id = $content_id and date = NOW() Then when you want to run a query for the most popular items in a specific time frame, simply run a query that looks for the highest views later than a certain date, and limit the results to however many items you want to show. SELECT * FROM views WHERE date > $limit_date ORDER BY views DESC LIMIT 5 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.