Reputation: 89
The idea is to show how many times people have visited a certain product page.
So far I've come up with two options.
Add a new column called "views" in the products table(Mysql). This seems to be the most simple and fastest way to setup.
Create a new table called "product_views". Have 3 columns for it. id, product id, views. From there you can use JOINS to update and call for view count.
So what I would like to know which option is better?
Upvotes: 2
Views: 1304
Reputation: 127
Ip address should be unsigned int(10), you convert the string to an number with INET_ATON. And similar reverse. Saves space and makes it much faster sort and filter on ip address. I have a table that looks like this:
CREATE TABLE statistics
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
productid INT(6) NOT NULL DEFAULT 0,
ip INT(10) UNSIGNED NOT NULL DEFAULT 0,
event char(1) NOT NULL DEFAULT 'V',
intervall TIME NOT NULL DEFAULT 0,
createddate TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id)
);
Upvotes: 0
Reputation: 902
Depends on what you want to go for. I usually log page views on a table such as:
ID (int), PRODUCT_ID (int), IP_ADDRESS (varchar(15)), VIEW_TIME (datetime)
and that gives me the ability to count either full views or unique views by IP ADDRESS and just do a COUNT(*)
on the table, and an added benefit of having a log of who visits the page. Obviously this will increase your space requirement needed for the DB.
If you're going to just create a product view column, then create a stored procedure that will take in the product id as a parameter and increase the value of the product_view column by 1 each time it's run.
Upvotes: 5