0
我想要计算某个文章已被评分的次数,例如我的成员已经对users_articles_id
3
进行了评分。PHP和MySQL问题
我也想算一定的文章例如users_articles_id
3
由其ratings_id
的等级分关系到ratings
数据库中的点应该是一个总的13
我不知道如果我是这样做是正确的,因为对我来说它看起来都错了?我希望如果有人能帮我解决这个问题?我的代码应该放在哪里?
我正在使用PHP和MySQL?
这里是我的MySQL表
CREATE TABLE articles_grades (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
ratings_id INT UNSIGNED NOT NULL,
users_articles_id INT UNSIGNED NOT NULL,
user_id INT UNSIGNED NOT NULL,
date_created DATETIME NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE ratings (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
points FLOAT UNSIGNED NOT NULL DEFAULT 0,
PRIMARY KEY (id)
);
数据库输入
articles_ratings
id ratings_id users_articles_id user_id date_created
1 3 2 32 2010-01-13 02:22:51
2 1 3 3 2010-01-13 02:23:58
3 2 3 45 2010-01-13 02:24:45
个评级
id points
1 10
2 3
3 5
这里是我试图解决的PHP代码。
// function to retrieve rating
function getRating(){
$sql1 = "SELECT COUNT(*)
FROM articles_ratings
WHERE users_articles_id = '$page'";
$result = mysql_query($sql1);
$total_ratings = mysql_fetch_array($result);
$sql2 = "SELECT COUNT(*)
FROM ratings
JOIN ratings ON ratings.id = articles_ratings.ratings_id
WHERE articles_ratings.users_articles_id = '$page'";
$result = mysql_query($sql2);
$total_rating_points = mysql_fetch_array($result);
if(!empty($total_rating_points) && !empty($total_ratings)){
// set the width of star for the star rating
$rating = (round($total_rating_points/$total_ratings,1)) * 10;
echo $rating;
} else {
$rating = 100;
echo $rating;
}
}
当你说你试图“修复”这段代码时,你遇到了什么问题? – AaronLS
目前尚不清楚问题是什么,你只是说'修复'。你能否详细说明你在上面的代码中遇到的问题。说这一切看起来错了有点通用。 – Camsoft