简体   繁体   中英

How to count page views in current day only?

I have multiple items on my website, each item has column totalviews and todayviews in database table.

I can work out page visit by adding this sql query to the page that contains the item for column totalviews and todayviews

mysqli_query($conn,"UPDATE memberpost SET totalviews = totalviews + 1 WHERE postid='".$row['postid']."'");
mysqli_query($conn,"UPDATE memberpost SET todayviews = todayviews + 1 WHERE postid='".$row['postid']."'");

But how can i reset column todayviews to 0 in the next day So that this column only contains number of visits in the current day only ?

Many thanks

If you're showing this as a report, I think your best try is, first, normalizing your tables by creating a table only for the access, and then adding a date column in it, so you can sum and filter later.

This way, you need to do only one query, an insert, with the postID and date.

Instead of adding +1 (todayviews = todayviews + 1) you simply set it to 0 (todayviews = 0)

mysqli_query($conn,"UPDATE memberpost SET todayviews = 0 WHERE postid='".$row['postid']."'");

You run this query whenever you want to set the count back to zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM