PHP:星级评分系统概念?

3
我正在使用PHP / MYSQL / JQUERY技术。
我有一个新闻板块。在新闻详情页中,我想添加一个星级评分系统,允许用户对新闻故事进行评分。我正在使用这个jquery评分系统http://www.fyneworks.com/jquery/star-rating/
现在,我不知道数据库结构是什么,以及我需要在PHP中编写什么代码。我需要了解应用的逻辑,例如,如果有1000人对文章进行了投票,有些人会给出2、3、1或5的评分。那么我应该把这些信息存储在哪里(数据库结构),并且需要在PHP代码中计算什么。
如果您有任何展示此概念的文章,请提供。
请帮助理解此逻辑和概念。
谢谢!
2个回答

3

以下是一个非常简单的 MySQL 示例:

drop table if exists image;
create table image
(
image_id int unsigned not null auto_increment primary key,
caption varchar(255) not null,
num_votes int unsigned not null default 0,
total_score int unsigned not null default 0,
rating decimal(8,2) not null default 0
)
engine = innodb;

drop table if exists image_vote;
create table image_vote
(
image_id int unsigned not null,
user_id int unsigned not null,
score tinyint unsigned not null default 0,
primary key (image_id, user_id)
)
engine=innodb;

delimiter #

create trigger image_vote_after_ins_trig after insert on image_vote
for each row
begin
 update image set 
    num_votes = num_votes + 1,
    total_score = total_score + new.score,
    rating = total_score / num_votes  
 where 
    image_id = new.image_id;
end#

delimiter ;

insert into image (caption) values ('image 1'),('image 2'), ('image 3');

insert into image_vote (image_id, user_id, score) values
(1,1,5),(1,2,4),(1,3,3),(1,4,2),(1,5,1),
(2,1,2),(2,2,1),(2,3,4),
(3,1,4),(3,5,2);

select * from image;
select * from image_vote;

明白了,哇,这种情况下我甚至不需要编写任何 PHP 代码吗?一切都会由 MySQL 处理,对吧?谢谢! - djmzfKnm
你需要一些 PHP 代码来将数据插入到 image_vote 表中,就这样 :) - Jon Black

1
ColorBox的制造商推出了一个名为ColorRating的jQuery/PHP评分系统,您可以使用它。由于ColorBox是一个很棒的程序,所以我认为ColorRating也会很好用。我建议您去查看一下,因为它可能会为您节省很多麻烦。

http://colorpowered.com/colorrating/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接