This example is a simple counter using graphics to display
the numbers. I call it simple because it will only show page views, not actual
visitors. It uses the File Scripting Object to interact with a text file. The
first thing it does is read the current number in the file and add one to it,
then displays it graphically. I am not a real graphics wizard and found these
numbers at Scream Design. There are many
places to get free graphics but, this is probably the best. I
use Left Right and Mid in this, so if you are not sure about them you will find
a detailed example here. I also use Len, explained
here.
And arrays discussed here. Now lets begin by looking at
the example.
We Have Had
Total Page Views
<%
'here we set up a connection to the txt file using a physical path
Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("C:\InetPub\wwwroot\counter\count.txt")
'now we read the number in the file
oldNum = CLng(oFile.ReadLine)
'and we close
oFile.Close
'we add one to the number
oldNum = oldNum + 1
'we open the file to overwrite the new number to it
Set oFile = oFSO.OpenTextFile("C:\InetPub\wwwroot\counter\count.txt",2,true)
'and we overwrite it here
oFile.WriteLine(oldNum)
'and we close the file again
oFile.Close
'now we use the number for our display
strCount = oldNum
'here we are looking at the length of the number (how long it is)
'because this will effect our display table. if it is one digit long like 9
If LEN(strCount) = 1 Then
'we set ali (align) to center
ali = "center"
'if its 2 or more digits long like 35 or 4255 we will align left
'we use this later
Else
ali = "left"
End If
%>
Below is a mix of html and vbscript. I will display the
script in red to make it more discernable.
in this line we are getting the legth of the number
again. this tells the table cell how many columns to span
<td colspan="<%=LEN(strCount)%>" align="center">We Have Had</td>
</tr>
<tr>
Now we only want to show as many cells and numbers as
we have data for. So if it is a two or more digit number we will show this cell
<%
If LEN(strCount) > 1 Then
%>
<td align="Right"><img border="0" src="<%=Left(strCount,1)%>.gif"></td>
<%
End If
'If it is a 3 or more digit
number we will show this cell as well
If LEN(strCount) > 2 Then
Dim numArray
i = (Len(strCount)-1)
For x = 2 to i
ReDim numArray(i)
%>
<td align="center"><img border="0" src="<%=MID(strCount,x,1)%>.gif"></td>
<%
Next
End If
%>
'It will always be at least a one digit
number so we allways show this cell. Also note in this line we have used ali
from above to set this cell to center or left. Remember we set that up above.
<td align="<%=ali%>"><img border="0"
src="<%=Right(strCount,1)%>.gif"></td>
</tr>
<tr>
<td colspan="<%=LEN(strCount)%>" align="center">Total Page Views</td>
</tr>
</table>
</center>
</div>
What we have done here is break up the number. Lets say it
is 123. The first cell in the table has this: <%=Left(strCount,1)%>.gif
Essentially, what we did was take the number 1 and use it to reference 1.gif
The second cell has this in it: <%=MID(strCount,x,1)%>.gif.
We used that to reference 2.gif
The third cell has: <%=Right(strCount,1)%>.gif.
This reference 3.gif.
With this in mind, you want to make sure all your images
are named corresponding to the number. So an image with 2 in it would be named
2.gif Got it?
Don't worry about how many digits long your number is. The
array that is built into this script will add as many numbers and cells in the
middle as it needs.
Now create a text file named count.txt and place it in a
directory named counter. Add your numbers to the web and you're all set!