This article mainly introduces the ranking example of php statistical articles. The code can be used on dedemcs to collect articles by month, week, or day. For more information, see
The Code is as follows:
/**
*
* Monthly, weekly, and daily rankings of software and articles
*
* $ Field_id (Article ID)
*/
// Method for monthly, weekly, and daily ranking
Require_once (dirname (_ FILE _). "/../include/common. inc. php ");
Function countdown ($ field_id ){
Date_default_timezone_set ('Asia/Shanghai'); // you can specify the default time zone.
Global $ dsql;
$ Re_total = 1;
$ TableName = '##__ tongji ';
$ NowDateArray = getdate (time ());
$ SQL _tongji = "select * from '$ tableName' where aid = $ field_id ";
$ Rs = $ dsql-> ExecuteNoneQuery2 ($ SQL _tongji );
// If this article does not exist, insert a new one.
If ($ rs <= 0 ){
// Obtain the column ID value
$ SQL _typeid = "select typeid from '#@__ archives' where id = $ field_id ";
$ T_row = $ dsql-> GetOne ($ SQL _typeid );
$ Query = "insert into '$ tableName' VALUES ($ field_id, $ t_row [typeid], 1, 1, 1, $ nowDateArray [0]);";
$ Dsql-> ExecNoneQuery ($ query );
} Else {
$ Result = $ dsql-> GetOne ($ SQL _tongji );
$ OldTimeStamp = $ result ['lasttime']; // last click Time
$ M_total = $ result ['m _ total']; // monthly click
$ W_total = $ result ['w _ total']; // weekly click
$ D_total = $ result ['d _ total]; // daily click
$ T_total = $ result ['t _ total]; // total clicks
$ OldDateArray = getdate ($ oldTimeStamp );
// Count the current month
If ($ nowDateArray ["year"] = $ oldDateArray ["year"] & $ nowDateArray ["mon"] = $ oldDateArray ["mon"]) {
$ M_total ++;
} Else {
$ M_total = 1;
}
// Statistics for this week
$ TmpStartDate = mktime (0, 0, $ nowDateArray ["mon"], $ nowDateArray ["mday"], $ nowDateArray ["year"]) -($ nowDateArray ["wday"] * 86400 );
$ TmpEndDate = mktime (23, 59, 59, $ nowDateArray ["mon"], $ nowDateArray ["mday"], $ nowDateArray ["year"]) + (6-$ nowDateArray ["wday"]) * 86400 );
If ($ oldTimeStamp >=$ tmpStartDate & $ oldTimeStamp <= $ tmpEndDate ){
$ W_total ++;
} Else {
$ W_total = 1;
}
// Count today
$ DayStart = mktime (, 0, $ nowDateArray ["mon"], $ nowDateArray ["mday"], $ nowDateArray ["year"]); // the start time of the day.
$ DayEnd = mktime (, 59, $ nowDateArray ["mon"], $ nowDateArray ["mday"], $ nowDateArray ["year"]); // end time stamp of the current day
If ($ oldTimeStamp >=$ dayStart & $ oldTimeStamp <= $ dayEnd ){
$ D_total ++;
} Else {
$ D_total = 1;
}
$ T_total ++;
// Update statistics
$ Dsql-> ExecuteNoneQuery ("update $ tableName set m_total = $ m_total, w_total = $ w_total, d_total = $ d_total, t_total = $ t_total, lasttime = $ nowDateArray [0] where aid = $ field_id ");
$ Dsql-> ExecuteNoneQuery ("update de_archives set click = $ t_total where id = $ field_id ");
$ Re_total = $ t_total;
}
Return $ re_total;
}
Countdown ($ aid); // method call
/*
// Mysql table structure
Create table if not exists 'dede _ tongji '(
'Id' int (11) not null,
'Cid' smallint (5) not null,
'Tid' smallint (5) not null,
'M _ total' int (11) not null default '1 ',
'W _ total' int (11) not null default '1 ',
'D _ total 'int (11) not null default '1 ',
'T_ total' int (11) not null default '1 ',
'Lasttime' int (12) not null,
Primary key ('aid ')
) ENGINE = MyISAM default charset = latin1;
*/
?>