|
|
|
Tutorial: Creating RateMySite
script |
| RateMySite scripts give your visitors
chance to rate your website. Ratings are from 1 which is poor to
5 which is excelent. Logic of this script can be also used for poll
script. |
| |
|
Database design
|
| For storing rating data we will
need just one table we can call it Ratings. This table need to have
three fields: rating_id an autonumber field, rating an numeric field
and and one text field for storing visitor IP. |
| |
|
Requirements to run
this script |
| Windows IIS web server,or any other
which have Asp support.Support for Microsoft Access database. |
| |
|
Script code
|
| The script itself
is developed to be included in asp page by using server side include
directive. Using SSI script file inc_ratemysite can be included
in any asp page.
At the begining of code the connection string
is defined and connection is opened.
|
dim
connStr
dim conn
dim rsRatings
connSTR = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ="
& Server.MapPath("_private\CaRateMySite.mdb")
set conn=server.CreateObject("ADODB.Connection")
conn.Open connSTR |
| |
| Then we need to check if visitor
posted his rating |
| |
rating=request("Rate")
if rating<>"" then |
| |
| if rating is posted we need to
check that this visitor did not already rated our page, we need
to check if cookie was issued and check the database against his
ip address. If cookie was not set and visitor ip is not in our database
we will store his rating and set a cookie. Using this we will prevent
same visitor to post multiple ratings. |
| |
visitorIP=request.ServerVariables("REMOTE_ADDR")
rating=request("Rate")
cookie=request.Cookies("Rate" & request.ServerVariables("SERVER_NAME"))
if cookie="" then
cookieRated=false
else
cookieRated=true
end if
Set rs = Server.CreateObject("ADODB.Recordset")
rs.ActiveConnection =conn
rs.Open "SELECT COUNT(*) FROM RATINGS WHERE IP='" &
visitorIP & "'"
if rs.Fields(0).Value=0 then
if cookieRated=false then
conn.Execute "INSERT INTO RATINGS(rating,ip) VALUES("
& RATING & ",'" & VISITORIP & "')"
RESPONSE.Cookies("Rate" & request.ServerVariables("SERVER_NAME"))=TRUE
RESPONSE.Cookies("Rate" & request.ServerVariables("SERVER_NAME")).EXPIRES=DATE()+30 |
| |
| Displaying the rating result.We
need to calculate the average rating by dividing the sum of all
ratings by number of votes. Average rating is rounded to integer.Rating
is presented by 5 stars.Shiny one for good rating and black one
or empty for unfilled rate. |
| |
set rsRatings=
Server.CreateObject("ADODB.Recordset")
rsRatings.ActiveConnection =conn
rsRatings.Open "SELECT SUM(RATING),COUNT(*) FROM RATINGS"
totalVotes=rsRatings.Fields(1).Value
if cint(totalVotes)>0 then
avgRating=rsRatings.Fields(0).Value/rsRatings.Fields(1).Value
finalRating=round(avgRating)
noStars=5-cint(finalRating)
else
finalRating=0
noStars=5
end if
rsRatings.Close
conn.Close
dim i
for i=1 to cint(finalRating)%>
Display the filled star or image of your choice
next
for i=1 to cint(noStars)
Display unfilled or black star or image of your choice
next |
|