PHP: star rating system concept?

I am using PHP / MYSQL / JQUERY.

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

Now I don’t understand what the structure of the database will be, and then what I need for the PHP code. I need what logic will be used for this, as if 1000 people voted for the article, some rating was 2 or 3 or1 or 5. Then where should I store (structure db) all this and what will I calculate (in php) .

If anyone has an article that shows this concept, please provide.

Please help to understand the logic and concept of this.

Thanks!

+3
source share
2 answers

here is 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
source

The creator of ColorBox has created a jQuery / PHP evaluation system that you can use with the name ColorRating. ColorBox is a great program, so I think that ColorRating will be as well. I would check this, as this can save you a ton of trouble:

http://colorpowered.com/colorrating/

+1
source

Source: https://habr.com/ru/post/1336115/


All Articles