Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Romain's avatar
Level 30

Number of views

Hi, I intented to have on my site on 1 or more resources, the number of times a resource has been viewed. As my resources are available through web or api, I'm expecting views per resource to be in the millions. I will want to show the number of views, maybe even differenciate the display by month/day/quarter... not sure yet. So I will need the view date What is the best way to save those views? Is SQL good for that? Should I simply save them in a table within the same DB as the rest of the site? Should I create a separate DB?

Thanks for any pointers

0 likes
4 replies
LaryAI's avatar
Level 58

To save the number of views for your resources, you can use a database to store the view data. SQL is a good choice for this purpose. You can create a separate table within the same database as the rest of your site to store the view information.

Here's an example of how you can structure your table:

CREATE TABLE resource_views (
    id INT AUTO_INCREMENT PRIMARY KEY,
    resource_id INT,
    view_date DATETIME
);

In this table, resource_id represents the ID of the resource being viewed, and view_date stores the date and time when the view occurred.

Whenever a resource is viewed, you can insert a new row into the resource_views table with the corresponding resource_id and the current date and time.

To differentiate the display of views by month, day, or quarter, you can use SQL queries to aggregate the view data based on the desired time period. For example, to get the number of views per month, you can use the following query:

SELECT MONTH(view_date) AS month, COUNT(*) AS view_count
FROM resource_views
GROUP BY MONTH(view_date)

This query will return the month number and the corresponding view count for each month.

Remember to index the resource_id column for better performance if you have a large number of resources.

By using a separate table for view data within the same database, you can easily query and manipulate the view information alongside the rest of your site's data.

Snapey's avatar
Snapey
Best Answer
Level 122

Update a cache item with an incrementing counter for the resource.

Have a job that once per day, writes the count for that day into the database along with the date.

Later you can pull all records for a resource and display counts by day, or summarise into month/quarter.

Make sure you don't routinely have some reason to clear the cache as you will lose counts for the day to that point.

Romain's avatar
Level 30

@Snapey Thank you for the answer. That makes a lot of sense and it will reduce the number of calls to the database... unless I want "live" updates (like on this forum for example)

Now, more like a business question but, should I also increment the view count when the index page is loaded? The index page containing multiple resources, should they all be +1ed? What would be your opinion on that? My resources are small and they can be viewed in their entirety from the indeix

Snapey's avatar

@Romain You can still display a live count from the cache?

You will have to make the call on the index. Does not seem right to increment a bunch of resources because someone was only interested in one.

Please or to participate in this conversation.