PHP: Star rating system concept?

2020-02-11 07:47发布

I am using PHP/MYSQL/JQUERY.

I have a site which have news section. On news details page, I want to add star rating system which will allow user to rate the news story. I am using this jquery rating system http://www.fyneworks.com/jquery/star-rating/

Now, I am not getting what will be the database structure and then what I need to code in PHP. I need what logic will be applied for this, like if 1000 people voted for the article, some given rating as 2 or 3 or1 or 5. Then Where I should be storing (db structure) this all and what I'll calculate (in php code).

If someone has any article which shows this concept please provide.

Please help, to understand the logic and concept of this.

Thanks!

2条回答
倾城 Initia
2楼-- · 2020-02-11 08:31

here's a very simple mysql example:

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;
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-11 08:37

The maker of ColorBox has made a jQuery/PHP rating system that you can use called ColorRating. ColorBox is a great program so I think that ColorRating would be as well. I'd check it out as it might save you a lot of trouble:

http://colorpowered.com/colorrating/

查看更多
登录 后发表回答