A rating system is a great way to get feedback from your site's visitors. In this article Annette shows us how to create a rating system with ASP and Microsoft Access.
An Article Rating System With ASP - Showing the ratings (Page 4 of 5 )
Let's assume that several visitors have rated our CD's. We might have 20 different ratings for each CD in the ratings table. If we have a cumulative rating total of 176 and 20 ratings for the first CD in the database, then we would work out the average rating like this:
Average Rating = Cumulative Rating Total / Number Of Ratings
... so the average rating for the first CD would be
Average Rating = 176 / 20 = 8.8
We want to round the ratings to the closet half number, so 8.8 would be rounded down to 8.5. We will be using ASP to work out the ratings shortly. Before we move on we need to edit showcds.asp. To display the ratings, we will be creating a new function called ShowRating at the end of the showcds.asp file. ShowRating will return the HTML code to display the rating for each CD in a graphical format.
Let's take a look at the ShowRating function:
function ShowRating(cdId)
const MIN_RATINGS_BEFORE_SHOW = 3
dim rs1 dim avgRating dim avgWhole dim decPart dim decCalc dim finalRating dim altText
First off we dimension the required variables and setup a constant variable called MIN_RATINGS_BEFORE_SHOW. This constant is used to determine how many ratings should exist in the ratings table before they are shown. For example, the default is 3, meaning that there must be at least 3 ratings in the ratings table before an average is worked out and displayed. This helps to stop visitor bias, etc.
We use a SELECT query to grab the sum of all ratings for the selected CD as well as the number of ratings using the COUNT function. If there are the same or more amount of ratings than MIN_RATINGS_BEFORE_SHOW then we workout the average rating and store it as the avgRating variable. If not, we display the text "[CD not rated yet]":
set rs1 = Server.CreateObject("ADODB.Recordset")
rs1.ActiveConnection = conn rs1.Open "SELECT SUM(rating), COUNT(*) FROM ratings WHERE cdId = " & cdId
if rs1.Fields(1) < MIN_RATINGS_BEFORE_SHOW then 'No ratings for this CD just yet Response.Write "[CD not rated yet]" else
'This CD has ratings, let's display the average avgRating = rs1.Fields(0).Value / rs1.Fields(1).Value
We need to determine the value of avgRating and whether or not it is a rating like 5 or a rating like 5.56666. If it's the latter, then we need to perform some rounding on the average rating. We workout the rounding like this:
If the decimal part of the rating is between .0 and .49 then it's rounded down to zero, so the resultant rating would be something like 5.0, 8.0, etc.
If the decimal part of the rating is between .5 and .99 then it's rounded down to .5, so the resultant rating would be something like 5.5, 8.5, etc.
Here's the code that handles the rounding of the average rating:
if Instr(1, CStr(avgRating), ".") > 0 then
'The average rating is a decimal, we need to either 'round the value up/down
if decPart <> "" then 'Work out whether or not we have to round this 'rating up or down
if CInt(decPart) >= 5 then decCalc = .5 else decCalc = 0 end if
finalRating = CInt(avgWhole) + CCur(decCalc)
else finalRating = avgRating end if else finalRating = avgRating end if
This leaves us with the finalRating variable, which will be something like 5, 6.5, etc. The only value after the decimal place will be .5.
We are going to be display the average rating as a series of images. For each positive rating point (for example, 5.5 has 5 positive points) we display one image. For each half point we display another, and for each negative point (for example 7 out of 10 has 3 negative points) another.
The images that we will use are called rating_on.gif, rating_half.gif, and rating_off.gif. They are contained within the support material for this article.
Before displaying the rating, we work out the text that will be set as the alt attribute for each image, meaning that we can move the mouse over the rating images to see the actual rating:
'Setup the alt text for the images altText = "Average visitor rating of " & finalRating & " out of 10"
for i = 1 to CInt(finalRating) Response.Write "<img alt='" & altText & "' src='rating_on.gif'>" next
if CInt(finalRating) <> finalRating then 'This article has a .5 rating, such as 5.5 Response.Write "<img alt='" & altText & "' src='rating_half.gif'>"
for i = CInt(finalRating)+2 to 10 Response.Write "<img alt='" & altText & "' src='rating_off.gif'>" next else for i = CInt(finalRating)+1 to 10 Response.Write "<img alt='" & altText & "' src='rating_off.gif'>" next end if
As you can see, we have three different loops: one to display the positive points, one to display the half point (if any), and one to display the negative points (if any). To determine whether or not this CD has a half point, we compare the integer value of the average rating to its normal value:
if CInt(finalRating) <> finalRating then 'This article has a .5 rating, such as 5.5 Response.Write "<img alt='" & altText & "' src='rating_half.gif'>"
So for example, if we had an average rating of 7.5 then we would compare the integer value of 7 to 7.5. They are different, and therefore there is a half point in this average rating.
Now that we've seen the ShowRating function, we simply place a call to it under where the title of each CD is dplsayed: