How to build simple reviewing and 5-star rating system? [closed]
Asked Answered
E

6

21

I'm very new to web technologies and this is basically for a term project that my team is working on. We are working on a food review site.

As of now, I'm not quite sure how to implement a simple 5-star rating system. Am I supposed to use a server-side language like PHP or a client-side one like Javascript (w/ JQuery). Looking around it seems that JQuery is more suitable for this? Or would it be a combination of both?

What I'm looking for as far as functionality goes is:

  • Stars light up when mouse hovers
  • Page doesn't have to be reloaded when a star is clicked (not really needed)
  • Some sort of average shown beside the stars
  • Rating has to be stored somewhere in a MySQL database (Is this a good idea?)

I really apologize if the question sounds vague and stupid, I don't have much of a clue on how to implement this and I've tried googling around. If you have any questions about it please let me know.

Thank you very much.

Encase answered 24/2, 2010 at 7:58 Comment(0)
W
20

Stars light up when mouse hovers
There's a brilliant tutorial on the web for designing a 5 star rating system: http://rog.ie/blog/css-star-rater. It's purely CSS so no need for Javascripting.

Page doesn't have to be reloaded when a star is clicked (not really needed)
Ajax is your friend, what I did was to have a <a class="one_star" href="javascript: submitRating(1, 5)"> where <a> represented a star and the submitRating() function used Ajax to transmit my rating (1/5) to the server, the server stores the rating (and assigns the user that gave the rating) and recalculates the new average rating and submit the results back in JSON format.

Some sort of average shown beside the stars
Easy. Write a SQL script that, based on the product id, takes the sum of the average rating (i.e. 1/5 + 2/5 + 4/5, etc), divide it(sum) by the total amount of ratings and multiplies it by 100. Return the value back to the server, and from the server back to the client.

Rating has to be stored somewhere in a MySQL database (Is this a good idea?)
I am using MySQL for this and it works like a charm....Any DB systems is fine.

Wildeyed answered 24/2, 2010 at 8:23 Comment(5)
Brief introduction to Ajax (en.wikipedia.org/wiki/Ajax_%28programming%29) and JSON (en.wikipedia.org/wiki/JSON)Wildeyed
AJAX, or even Javascript, is not necessary to not reload the page: #17386083Thirion
@Uday Sawant, the links works now. I guess the author of the blog wanted to revamp it.Wildeyed
What about performance ? If I have millions and millions of ratings to sum up ?Bagatelle
Link broken again.Torrefy
T
3

Stars light up when mouse hovers
Simple, have an image for yellow star and an image for white star. When a star is hovered, all stars to the left have their "src"s changed to the yellow star, all stars to the right have their srcs changed to the white star.

Page isn't reloaded when star is clicked
Look into Ajax/XHR
http://en.wikipedia.org/wiki/Ajax_(programming)
Hint: Have a .php file called vote.php , and pass into it the ID of your food and the rating given by the user.

Some sort of average shown between the stars
Most websites only show 0%, 50%, and 100% stars... users won't really freak out if they don't get 'exact precision'

Rating should be stored in MySQL database
Any database system is fine. But yes, you would probably want to use an SQL database for this

Transmundane answered 24/2, 2010 at 8:3 Comment(1)
For your first answer, it can be done using CSS...See my answer.Wildeyed
T
2

Here's an answer to a similar question and should be a great start.

Turn a number into star rating display using jQuery and CSS

Trina answered 24/2, 2010 at 8:15 Comment(0)
C
0

Yeah you're approach sounds perfect. there are a couple pieces:

stars with hover effect:

jquery: http://www.fyneworks.com/jquery/star-rating/

prototype: http://www.fyneworks.com/jquery/star-rating/

css: http://www.henryhoffman.com/css-star-rating-tutorial.html

onclick can invoke an ajax call with jquery: http://api.jquery.com/jQuery.post/

then on the server side it can be maintained in a rating table

theres a longer example here:

http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/

Cunaxa answered 24/2, 2010 at 8:4 Comment(0)
G
0

A combination of JS and PHP is the way to go (if PHP is your chosen server-side language). JQuery is a good way to easily implement a sort of starring system, I once developed this for one of my sites I later removed. It was a simple thing that had onrollover events linked to 5 images. When clicked, if it was "id 4", it'd send that as the vote of the user using jQuery's .post() function to a PHP script which noted the user and recorded the vote in a MySQL database if they hadn't already voted.

Grapher answered 24/2, 2010 at 8:4 Comment(0)
M
0

To have a nice star-clicky control, yeah you need javascript. So you should do jQuery.

Yes, you'll want to store stuff in a database. You could use PHP and MySQL for that.

Given this is a class assignment, thats a lot to implement. Consider cheating by using an existing CMS. Drupal in particular has functionality similar to what you ask for. You won't have to learn much of Drupal, just buy the book "Using Drupal". Chapter 4 will show you how to:

... build a community product review website, with ... and Fivestar modules providing a rating widget.

Drupal is PHP based and uses MySQL as its database, so it seems to match where you were going. It may take a bit to get Drupal running... but then you can get to chapter 4 in a snap.

If the teacher insists you write code, Drupal is open source and its easy to add custom modules.

Mcgee answered 24/2, 2010 at 8:8 Comment(1)
You don't have to do it with just jQuery...I used simple ajax and JS...no biggie!Wildeyed

© 2022 - 2024 — McMap. All rights reserved.