Jump to content

Counting Database Entries


drewdan

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/272451-counting-database-entries/
Share on other sites

  On 12/28/2012 at 5:23 PM, drewdan said:

Does that logic make sense?

No.

 

  On 12/28/2012 at 5:23 PM, drewdan said:

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.