jonboy
jonboy

Reputation: 2749

Counting views of certain page

I am developing a library app using a php, html and a custom mvc framework that contains 1000's of books, I would like to show the 3 most viewed books on my home page (and various other pages).

I will be creating a module to show on various pages.

What is the best way to approach this? I'm not sure how to collect such information. I don't mind if the views are unique or returning visits, not really important.

My favourite table structure is as follows;

-------------------------------------------
id    title   category    author    isbn
1     book 1   art        joe       123123
2     book 2   art        jim       234234
3     book 3   science    james     456456
4     book 4   maths      sarah     653335
5     book 5   spanish    jim       34534
...
...
-------------------------------------------

Do I need to make another column in my sql table and store the views here? Are the views stores in a session variable? Just looking for a simple solution really.

Quite new to php/sql here so any advice and/or direction is appreciated.

Upvotes: 0

Views: 72

Answers (2)

thedouglenz
thedouglenz

Reputation: 514

You could also create a "views" table with columns like datetime, book_id, and any other data you want to collect about the client. add a new row to that table every time a user accesses a view book page. Then you just need a query like:

select count(*) from views where book_id = ...

Upvotes: 0

Lorenzo Marcon
Lorenzo Marcon

Reputation: 8179

You could add an integer column (let's call it views). Every time a user opens a book page, increment that column by one with a simple update query.

Then you can retrieve the three most viewed book with the following query:

select * from favourite order by views desc limit 3

Upvotes: 6

Related Questions