How to add number of views in database in php mysql
How do we add number of views ( eg most 开发者_运维技巧viewed articles) in php mysql database. How to add numbers when a person click to database?
There are a couple different options. If you have a Sproc that gets your data you could have it increment the counter every time it does so. That would not give you unique views, but i think it would do the trick for you.
You could run the update yourself, maybe base it off a session value to make it a unique view. If you want more details on either of these, let me know.
Several options.
Keep a log file for all request, along with some information that can be used to identify certain resources, e.g. articles - the query this global log for page view stats.
On each request of one article, increment an integer field - something like
view_count
- in your table. Here, the data is held locally, which is certainly faster (but leaves your request data a little spread over your database, in case you want to track other view counts, too - not just for articles).
Keep in mind, that if you want to track unique visitor views, you might need to store some additional information in your session, since you don't want to count simple page reloads as page views.
Simplest solution:
Let's pretend your pages table is called "pages", and the number of views column is called "views".
Every page should trigger the following query:
UPDATE pages SET views=views+1 WHERE ID=19
(19 is the visited page ID)
精彩评论