Home > Article > Backend Development > How to use php to count recent popular posts
For example, my website wants to count the most popular posts in the past 7 days, which are the posts with the most visits in the past 7 days. How to use PHP to design it? I have never been able to think of a good way. I hope you can give me some advice. In fact, it is similar to the hot post function of discuz
For example, my website wants to count the most popular posts in the past 7 days, which are the posts with the most visits in the past 7 days. How to use PHP to design it? I have never been able to think of a good way. I hope you can give me some advice. In fact, it is similar to the hot post function of discuz
You can use the timing function to perform statistics every
<code>例如: 帖子热度=访问次数*1+收藏*10+点赞*3+评论*5 </code>Then the result sorting stores its sorting into the data table or cache.
Increments must be reflected when designing the database. In the past, a field was added directly to the article table to indicate the number of visits. In this case, the access information must be stored in a separate table, and each visit will be treated as a record
Perform auto-increment operation based on article ID redis. Page display sorted in reverse order
The data table records the posting time: time, and the number of views: views. Views increase with the number of views.
Get the timestamp range of yesterday and the previous seven days.
<code class="php">public function last_unix($a) { $y = date("Y"); $m = date("m"); $d = date("d")-$a; $start= mktime(0,0,0,$m,$d,$y); $end= mktime(23,59,59,$m,$d,$y); return array('start' =>$start ,'end'=>$end ); } $yesterday = $this->last_unix(1); $lastsevernday = $this->last_unix(7); $hotest=$a->where("time>'$lastsevernday' AND time<'$yesterday'")->max('views');</code>