PHP Statistics Article ranking Example _php tutorial

Source: Internet
Author: User
Copy CodeThe code is as follows:
/**
*
* Statistics software and articles, such as month, week, day ranking
*
* $field _id (article ID)
*/
The method of counting month, week and day ranking
Require_once (DirName (__file__). " /.. /include/common.inc.php ");
function countdown ($field _id) {
Date_default_timezone_set (' Asia/shanghai '); Set the default time zone
Global $dsql;
$re _total = 1;
$tableName = ' Dede_tongji ';
$nowDateArray = getdate (Time ());
$sql _tongji = "select * from ' $tableName ' where aid= $field _id";
$rs = $dsql->executenonequery2 ($sql _tongji);
If this article information is not present, a new one is inserted
if ($rs <= 0) {
Get column ID value
$sql _typeid = "Select typeID from ' dede_archives ' where id= $field _id";
$t _row = $dsql->getone ($sql _typeid);
$query = "INSERT into ' $tableName ' VALUES ($field _id, $t _row[typeid],1,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 ']; Month Click
$w _total = $result [' W_total ']; Week Click
$d _total = $result [' D_total ']; Day Click
$t _total = $result [' T_total ']; Total Hits
$oldDateArray = getdate ($oldTimeStamp);

Statistics month
if ($nowDateArray ["year"] = = $oldDateArray ["Year"] && $nowDateArray ["mon"] = = $oldDateArray ["Mon"]} {
$m _total++;
}else{
$m _total = 1;
}

Statistics this week
$tmpStartDate = Mktime (0,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;
}

Statistics today
$dayStart =mktime (0,0,0, $nowDateArray ["mon"], $nowDateArray ["Mday"], $nowDateArray ["Year"]); Day start timestamp
$dayEnd =mktime (23,59,59, $nowDateArray ["mon"], $nowDateArray ["Mday"], $nowDateArray ["Year"]); End time stamp of the 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 dede_archives set click= $t _total where id= $field _id");
$re _total = $t _total;
}
return $re _total;
}

Countdown ($aid); Method invocation

/*
MySQL table structure

CREATE TABLE IF not EXISTS ' Dede_tongji ' (
' Aid ' int (one) is not NULL,
' CID ' smallint (5) Not NULL,
' Tid ' smallint (5) Not NULL,
' M_total ' int (one) not NULL DEFAULT ' 1 ',
' W_total ' int (one) not NULL DEFAULT ' 1 ',
' D_total ' int (one) not NULL DEFAULT ' 1 ',
' T_total ' int (one) not NULL DEFAULT ' 1 ',
' Lasttime ' int (a) is not NULL,
PRIMARY KEY (' aid ')
) Engine=myisam DEFAULT charset=latin1;
*/
?>

http://www.bkjia.com/PHPjc/736801.html www.bkjia.com true http://www.bkjia.com/PHPjc/736801.html techarticle Copy the code as follows: PHP/** * * Statistical software and articles, such as month, week, day ranking * * $field _id (Article ID) * *//Statistics month, week, the day ranking method require_once (DirName (_ ...

  • 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.