Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple counter issue

Status
Not open for further replies.

SteveHigh

Technical User
Jan 17, 2007
158
0
0
GB
Hello

I have a counter, that is, a couple of files (example.asp and hit_counter.inc), plus a mdb on my server.

The important example.asp code is:

<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>

while in the hit_counter.inc, I have:

<%
'This function returns an open connection to the access database.
Function GetDBConnection()
Dim databasePath, connectionString
'MODIFY: Specify the relative path to the database file.
databasePath = ".\HitCounter.mdb"

'Connect to the Access database using a DSN-Less connection
Set GetDBConnection = Server.CreateObject("ADODB.Connection")
connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=" & Server.MapPath(databasePath)
GetDBConnection.Open connectionString
End Function

'This function will return the current hit count for the page specified by pageName.
' If incrementHitCount (boolean) is true, the it will first add one to the current hit count.
Function GetCurrentHitCount(pageName, incrementHitCount)
Dim con, rs, sql, currentHitCount
'Get the database connection
Set con = GetDBConnection()

'Get the current hit count
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * FROM tblHitCount WHERE PageName = '" + pageName + "'"
rs.Open sql, con, 2, 3

If (rs.EOF) Then
'No count was found, create a new record and start the count at 0
rs.AddNew()
rs("PageName") = pageName
rs("CurrentHitCount") = 0
rs.Update()
End If

'Get the current hit count from the database
currentHitCount = rs("CurrentHitCount")

If (incrementHitCount) Then
'Add one to the current hit count before retrieving it
currentHitCount = currentHitCount + 1
rs("CurrentHitCount") = currentHitCount
rs("LastAccessed") = Date()
rs.Update()
End If

'return the hit count
GetCurrentHitCount = currentHitCount

'Clean up database objects
rs.Close()
con.Close()

Set rs = Nothing
Set con = Nothing
End Function

%>

They both work fine. However, when I paste the script from example.asp, that is,

<div align="center">
This page has been accessed<br />
<%=GetCurrentHitCount("example", true)%><br />
times
</div>

into my own home page, I cannot see the number of 'hits' (it is blank).

The problem may be related to the database connection, yet this was not a problem when example.asp and hit_counter.inc worked together - the problem occurs only when I copy and paste the script from example.asp into my own page.

All files (my own home pages and the above example.asp, hit_counter.inc and mdb files are all on the server in one directory.

Any ideas, please?

Thanks.

Steve

 
hi steve - have tried your code and works fine

i assume you are including the function page in your main page otherwise you would be getting errors - defo no errors??

is the value in the database being stored and updated?

Code:
<!--#include file="hit_counter.asp"-->

can you see the text 'times' - try highlighting the area to see if its a formatting issue

 
Hello Thompom

Many thanks for your reply.

I have got it working now. I had not inserted
<!--#include file="hit_counter.asp"-->
above <html> in my page!!

Thanks for drawing my attention to it.

Cheers

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top