Whether your site sells products or provides content, having a rating system in place, where visitors can quickly rank what they bought or read, is tremendously useful and takes little time or effort to implement. Ratings can help customers make purchasing decisions and help site administrators know what products or articles are worth promoting and which ones ought to be cast aside. The success of such systems depends upon visitors actually using it, which means it must be simple and reliable. Ajax, when done right, is therefore the perfect tool for the job.
To start, you’ll need to add a database table to the site, something like:
CREATE TABLE ratings ( `rating_id` INT UNSIGNED NOT NULL AUTO_INCREMENT, `item_id` INT UNSIGNED NOT NULL, `rating` TINYINT UNSIGNED NOT NULL, `date_created` TIMESTAMP NOT NULL, PRIMARY KEY (`rating_id`) )
That SQL command represents the crux of such a table. The two most important columns are the rating, which will be an unsigned tiny integer (perhaps from 1 to 5), and the item_id. Most likely, your particular column won’t be called item_id, but perhaps page_id or product_id or whatever. The important thing is that item_idwhatever you call itacts as a foreign key to the thing on your site that’s being rated, be it a product or a page of content.
The date_created column isn’t required, but I generally like to timestamp almost every record added to every database. You never know when that information will come in handy!
Important decisions to be made involve who can rate content and how many times. By the latter I mean: Will a system be implemented for guaranteeing (or attempting to guarantee) that a person doesn’t rate the same item multiple times? Doing so isn’t hard, but requires some thought.
The first option is not to reflect the rater at all, trusting that the same person won’t make rate an item multiple times (or, if they do, it’s not a big deal). On the opposite end of the spectrum, perhaps only registered (and therefore logged-in) users can provide ratings. In that case, the ratings table should also store the user ID, or whatever represents a foreign key to the applicable users table. You would also change the table’s definition to place a unique index on the combination of the user’s ID and the item ID, or just use those two columns as the primary key, which will have the same effect:
CREATE TABLE ratings ( `item_id` INT UNSIGNED NOT NULL, `user_id` INT UNSIGNED NOT NULL, `rating` TINYINT UNSIGNED NOT NULL, `date_created` TIMESTAMP NOT NULL, PRIMARY KEY (`item_id`, `user_id`) )
If registration is not required in order to rate items, which is really for the best, you’ll need another way to identify users. Probably the best solution would be to store a cookie in the user’s browser with a unique ID. You could then store the ID in the ratings table, under the user_id field, but most likely as a CHAR(32). The PRIMARY KEY index would still apply, preventing the same person from rating the same item more than once (you’d want to add PHP code to the page reflecting the visitor’s previous rating anyway).
The next consideration is a user interface issue: How will the visitor indicate his or her rating? The simple route is a select menu or series of radio buttons. With the right CSS, that can look pretty good. Alternatively, you can use a series of images: stars or the like. Which route you go will change how you paste Ajax onto the system. In the rest of the article, I’ll discuss the form option.