drewdan Posted December 28, 2012 Share Posted December 28, 2012 Hi Guys, Just want to run past an idea I am having, I want to make sure the logic is sound. Basically, I am making a website statistics feature for my Content Management System. I want to create a function that will work out the most visited pages on the website. I currently collect a multitude of data, which is stored in a table in my database. Each time a person loads a page the data is added to the table. Some of that data includes the page id. My plan is call this data from the database and add the data to an array. As the rows of the table are being looped through, the script would check to see if the pageid is already in the array, and increment the count, and if not add it to the array until it has collected all of the logs. The array would then be sorted and the top 10 pages would be shown in a graph on the website. Does that logic make sense? Is there a simpler way of doing it? Any input would be great. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/272451-counting-database-entries/ Share on other sites More sharing options...
Psycho Posted December 28, 2012 Share Posted December 28, 2012 Does that logic make sense? No. Is there a simpler way of doing it? Yes. Just do a query to do all that work for you. Here is an example that will return the top 10 most visited pages (assuming there is a separate record for each page hit) SELECT pageID, COUNT(pageID) as hits FROM stats_table GROUP BY pageID ORDER BY hits DESC LIMIT 10 Quote Link to comment https://forums.phpfreaks.com/topic/272451-counting-database-entries/#findComment-1401860 Share on other sites More sharing options...
drewdan Posted December 28, 2012 Author Share Posted December 28, 2012 Well, im glad I asked. Thank you, that code worked a treat! Quote Link to comment https://forums.phpfreaks.com/topic/272451-counting-database-entries/#findComment-1401862 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.