859Chapter 45A User-Rating SystemLinking ratings with contentSo now (Kids web site)

859Chapter 45A User-Rating SystemLinking ratings with contentSo now we have a table representing possible ratings and a table representing the content tobe rated. How shall we associate them? We ve already made a decision of sorts by making two separate tables. If each piece of contentonly received one rating, we could have added the rating value as a column in the contenttable. Similarly, if each rating were applied to only one piece of content (as with finishing orderin a race), we could have made the content ID a column in the ratings table. As it is though, wehave multiple pieces of content, which different users will associate with different ratings. Thisis a many-to-many relationship, so we will need a third table to capture the associations. We want each row in this ratingstable to represent an instance of a content item receiving a rating. At a minimum, then, we need to identify both the content and the rating in each row. We ll do this by using the primary keys from each table. In addition, we ll throw in a fewcolumns that we have found to be useful, even if we won t be using them much in this chap- ter s code. create table ratings (ID int primary key auto_increment, rating int, rated_id int, rating_date timestamp, user_ip varchar(30), bogus_bit tinyint default 0); The first three columns are the minimum we need: our usual auto-incremented primary keyand the IDs from the two tables we are associating. The next two will be used to capture someinformation about the rating event: the time it happened, and the IP address of the user doingthe rating. (This last one we include not for any evil, privacy-invading purpose, but justbecause it turns out to be useful in combating mass ballot stuffing. If you receive a negativereview for an item once every two seconds, it can be useful to know that all those votes arecoming from the same place.) Finally, we include a bit we can flip if we conclude that largenumber of rows are bogus without deleting them, we can write code to screen them out ofvote totals and displays. Now we have designed our three tables and have populated only one of them (rating_ values). It seems wasteful to print sample quotes that make up the entries for the quotationstable (although we will include them in the database dump for this chapter found at www. troutworks.com/phpbook). Finally, we have not yet populated the ratings table, becausethat is something that our users should do. Collecting VotesTo let our users vote on our content, we need to display that content alongside some kind ofform that lets them express their feelings. In this section, we ll create a content page that displays one item and encourages the user to rate it. The code for this page is shown inListing 45-1. This is a high-level code file, which includes other function files, which in turn do most of the work.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Leave a Reply