このスクリプトを毎日(cron)実行して、1か月のフィードバックの合計量を更新する必要があります(少なくとも、これは私が今考案したものです)。これが私が持っているコードです。誰かが私がこれについてどうやって得るべきかについてより良い考えを持っていますか?たぶん、これについてのやり方を変えたり、updateMonthlyFeedback.phpスクリプトを最適化したりしますか?
updateMonthlyFeedback.php
session_start();
include("db.php");
$sql="SELECT MAX(uid) as maxUID FROM users";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$maxUID = $row['maxUID'];
for($i=0;$i<$maxUID;$i++){
$sql="SELECT COUNT(*) as negativeCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = -1 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$negativeCount = $row['negativeCount'];
$sql="SELECT COUNT(*) as neutralCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = 0 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$neutralCount = $row['neutralCount'];
$sql="SELECT COUNT(*) as positiveCount FROM feedbacks WHERE date_created >= ( CURDATE() - INTERVAL 30 DAY ) AND type = 1 AND uid = '$i'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
$positiveCount = $row['positiveCount'];
$sql = "UPDATE feedback_totals SET negativeCount = '$negativeCount', neutralCount = '$neutralCount', positiveCount = '$positiveCount' WHERE uid = '$i'";
$result=mysql_query($sql) or die(mysql_error());
}
MySQLテーブル
CREATE TABLE feedback_totals (
uid VARCHAR(40),
negativeCount int,
neutralCount int,
positiveCount int,
halfStarCount int,
oneStarCount int,
oneHalfStarCount int,
twoStarCount int,
twoHalfStarCount int,
threeStarCount int,
threeHalfStarCount int,
fourStarCount int,
fourHalfStarCount int,
fiveStarCount int,
PRIMARY KEY (uid)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);
CREATE TABLE feedback_last_month (
uid VARCHAR(40),
negativeCount int,
neutralCount int,
positiveCount int,
halfStarCount int,
oneStarCount int,
oneHalfStarCount int,
twoStarCount int,
twoHalfStarCount int,
threeStarCount int,
threeHalfStarCount int,
fourStarCount int,
fourHalfStarCount int,
fiveStarCount int,
PRIMARY KEY (uid)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);
CREATE TABLE feedback (
feedback_id INT NOT NULL AUTO_INCREMENT,
uid VARCHAR(40),INDEX (uid),
sender_id VARCHAR(40),
type int, #-1 = neg, 0 = neutral, 1 = positive
starCount VARCHAR(40),
description VARCHAR(80),
date_created timestamp DEFAULT CURRENT_TIMESTAMP,
fromType VARCHAR(40), # buyer or seller
fromUsername VARCHAR(40),
PRIMARY KEY (feedback_id)
#FOREIGN KEY (uid) REFERENCES users(uid) ON DELETE CASCADE
);