ASP
  Home arrow ASP arrow Page 3 - A Simple Way To Tally Your Visitors
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

A Simple Way To Tally Your Visitors
By: Burag Cetinkaya
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 8
    2002-01-20

    Table of Contents:
  • A Simple Way To Tally Your Visitors
  • Creating our hit counter
  • Displaying the hit counter
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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 Select


    The 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 If


    We 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 If


    This 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.Close


    Firstly, 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>"

    Next


    We 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 ImgSequence

    Lastly, 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 If


    Including our counter script

    Now 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:

    Displaying our hit counter as text

    Displaying our hit counter as images

    More ASP Articles
    More By Burag Cetinkaya


     

    ASP ARTICLES

    - Central Scoreboard with Flash and ASP
    - Calorie Counter Using WAP and ASP
    - Creating PGP-Encrypted E-Mails Using ASP
    - Be My Guest in ASP
    - Session Replacement in ASP
    - Securing ASP Data Access Credentials Using t...
    - The Not So Ordinary Address Book
    - Adding and Displaying Data Easily via ASP an...
    - Sending Email From a Form in ASP
    - Adding Member Services in ASP
    - Removing Unconfirmed Members
    - Trapping HTTP 500.100 - Internal Server Error
    - So Many Rows, So Little Time! - Case Study
    - XDO: An XML Engine Class for Classic ASP
    - Credit Card Fraud Prevention Using ASP and C...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT