Weekly ranking, monthly ranking Development Summary (original) _ PHP Tutorial

Source: Internet
Author: User
Tags mysql functions
Weekly ranking, monthly ranking Development Summary (original ). Prerequisites: groupby, MYSQL functions week () and month () generally have a field to record the article's CTR when designing the database, if we want to count the rankings of click rates for one week or one month: group by, MYSQL function week (), month ()

When designing a database, there is usually a field to record the article's Ctr. if we want to calculate the weekly or monthly CTR rankings, this field is definitely not feasible. Create a new table to record the daily click rate of each article.

Assume that this table is named ranking and defines four fields: rid (table ID), contentid (associated with the article ID), hits (record the daily click rate), date (time, important, comparison during query)

Ranking structure

Id contentid hits date

1 2 12 2010-12-18

2 2 23 2010-12-19

3 1 15 2010-12-19

4 2 21 2010-12-20

I. Statistics

The first step is to record the article's daily click rate. This step is very simple. when you view an article, the PHP program will perform a database query to determine whether the record exists. if it does not exist, this is the first time you browse this article on the current day. you only need to update the click rate when you insert a record. This is to record the click rate of an article in a day.

PHP:

$ Date = date ("Y-m-d", time ());

$ Contentid = $ _ GET [id]; // The ID of the current article.

$ Query = mysql_query ("select * from ranking where contentid = $ contentid and date = $ date); // query the database

If ($ value = mysql_fetch_array ($ query )){

Mysql_query ("update ranking set hits = hits + 1 where id = $ value [id]"); // if there is a record, click rate + 1

} Else {

Mysql_query ("insert into ranking ('contentid', 'hits ', 'date') values ($ contentid, 1, $ date)"); // if it is the first time you browse, insert a data entry with a click rate of 1

}

II. query

Now that the statistics have been completed, it is difficult to query these articles in the order of the total click rate of one week or one month.

1. group the article and calculate the total CTR: select *, sum (hits) from ranking group by contentid order by sum (hits) desc

2. filter data from this week: select *, sum (hits) from ranking where week (date) = week (now () group by contentid order by sum (hits) desc

This is a weekly ranking query statement, which is relatively complex. after the query is made, it is displayed in the array in sequence. The Monthly ranking is also like this. just change the function, I will not write the complete PHP code.

Http://bbs.2cto.com/mode.php? M = o & q = user & u = 53700.

Sort by, MYSQL functions week (), month () usually have a field to record the article's CTR when designing the database. if we want to count the ranking of CTR for one week or one month...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.