A Simple Way To Tally Your Visitors - Displaying the hit counter
(Page 3 of 4 )
As mentioned earlier, we will have the option to display our hit counter as either a set of numerical images, or as plain text. We do this by passing a query string value to our hit_counter.asp script when we display it on the page (more on this soon).
Our script uses a "select case" statement to determine which method we want to use to display our hit tally:
Select Case Request.QueryString ("Mode")
Case "Text"
'Code to display text counter goes here
Case "Graphic"
'Code to display graphic counter goes here
End SelectThe code for the text option of our select case statement looks like this:
'Count all visits in the database
RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3
If Not Rs.Eof and NOT Rs.Bof Then
'Write the total number of visits
Response.Write Rs("TotalVisits")
Else
'No Records in the database
Response.Write "0"
End IfWe start by executing a query and storing the results in our record set object in a single field named TotalVisits. The query uses SQL's count function to retrieve the number of rows in our hits table. If a row was returned from the query then we output the value of its TotalHits field to the using ASP's Response.Write function. If our query returned no rows, then we output 0 to the browser.
Note that the code between the else block will never actually execute unless there is an error with our query, because we have placed the code to add a record to the hits table at the top of the page.
Before we look at the output from the code I've just described, let's take a look at the code to display the tally graphically:
'Count all visits in the database
RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3
If Not RS.Eof and NOT RS.Bof Then
'Store the total number of visits in TotalVisited Varable
TotalVisits = RS("TotalVisits")
RS.Close
'Now we will create the sequence of gif files
For I = 1 to Len (TotalVisits)
If I <> 1 Then
TotalVisits = Right (TotalVisits, Len (TotalVisits) – I)
End If
CharToReplace = Left (TotalVisits, 1)
ImgSequence = ImgSequence & "<img src='" & CharToReplace & ".gif' Border=0>"
Next
'This will write a series of <img> tags on the html page.
Response.Write ImgSequence
Else
'No Records in the database
Response.Write "<img src='0.gif' border=0>"
End IfThis code is a little bit more complex that the code to display the hit tally as text, so let's run through it step-by-step:
RS.Open "SELECT COUNT (*) AS TotalVisits FROM Hits", conn, 3, 3
If Not RS.Eof and NOT RS.Bof Then
'Store the total number of visits in TotalVisited Varable
TotalVisits = RS("TotalVisits")
RS.CloseFirstly, we execute the same query that we did to get our hit tally as text, returning the number of rows in the hits table. If Access returned a row, then we get the value of its TotalVisits field into a variable and close the record set.
'Now we will create the sequence of gif files
For I = 1 to Len (TotalVisits)
CharToReplace = Mid(TotalVisits, I, 1)
ImgSequence = ImgSequence & "<img src='" & CharToReplace & ".gif' Border=0>"
NextWe loop through each character in the TotalVisits variable. Remember that variables in ASP are variants, so ASP will treat our variable as a string in this case. We grab each character in the TotalVisits variable using the mid function, save it to another variable named CharToReplace, and then remove it from the TotalVisits variable.
The signature of the mid function looks like this:
variant Mid(string s, pos p, length l)So in our for…next loop, we retrieve each character successively. We have a variable named ImgSequence to which we append an image tag for each individual character in the TotalVisits variable. These image tags can range from 0 to 9 and are named 0.gif, 1.gif, 2.gif, etc. They are available as part of the support material for this article from the last page and should be saved into the same directory as the hit_counter.asp script. So, for example, if we had a visitor tally of 31, then the ImgSequence variable would look like this at the end of the for…next loop:
<img src='3.gif'><img src='1.gif'>The value of the ImgSequence variable is output to the browser using the Response.Write function:
Response.Write ImgSequenceLastly, if no records were returned from our SQL query by Access, then we display the image 0.gif, meaning that no one has visited our site:
Else
'No Records in the database
Response.Write "<img src='0.gif' border=0>"
End IfIncluding our counter scriptNow that we have our database setup and have a full explanation of the hit_counter.asp file, it's really simply to display the hit counter on our page. We can use ASP's #include directive, to display the text counter like this:
<!--#include file="hit_counter.asp?Mode=Text" -->Or, we can display the graphical counter, like this:
<!--#include file="counter.asp?Mode=Graphic" -->Here's a screen shot for each method:


Next: Conclusion >>
More ASP Articles
More By Burag Cetinkaya