Forums

This topic is locked

Creating a simple counter.....

Posted 13 Dec 2001 22:46:26
1
has voted
13 Dec 2001 22:46:26 Mitchel Tendler posted:
All I want to do is track everytime a page is viewed.

I'd like to add some type of ASP code to the top of every ASP page so it
sends the name of the page to a database (harcoding the name of the page, on each page is acceptable). I would then create an ASP page that counted the times each page was viewed and then display the info
grouped by page name.

The problem is that I cannot figue out the ASP to do this.

Sometimes the easiest things are HARD to figure out.

Help? <img src=../images/dmxzone/forum/icon_smile.gif border=0 align=middle>

When in doubt...reboot!

Replies

Replied 14 Dec 2001 00:03:00
14 Dec 2001 00:03:00 Owen Eastwick replied:
I had a similar requirement recently, my solution was to use a bit of ultracart code. Here's what I came up with:

set UC_rsId = Server.CreateObject ("ADODB.Recordset"
UC_rsId.ActiveConnection = MM_YourConnection_STRING
UC_rsId.Source = "SELECT YourDatabaseField FROM YourDatabaseTable"
UC_rsId.CursorType = 0 ' adOpenForwardOnly
UC_rsId.CursorLocation = 2 ' adUseServer
UC_rsId.LockType = 2 ' adLockPessimistic
UC_rsId.Open
varYourHitCounter = UC_rsId.Fields("AdReference".value
UC_rsId.Fields("YourFieldName".value = varYourHitCounter + 1
UC_rsId.Update
UC_rsId.Close
set UC_rsId = Nothing

Obviously this code will just update one field within a table, so you could modify it slightly to add 1 to a counter for each page. Try modifying the SELECT statemtent something like this:

UC_rsId.Source = "SELECT YourDatabaseField FROM YourDatabaseTable WHERE PageID = 7"

You could then set up a table in the database something like this:

PageID - PageName - HitCount

Then give each page an ID number, copy and paste the code into each page and just change the PageID value. Should do the trick.

I used the Ultracart code because it only allows one person to update the counter at any time (locktype = pessimistic) so that in the event that 2 or more people people access the same page at the same time there are no problems with two people updating to the same number and making the count useless. In effect what happens is that each is denied access to the table until the complete read - update operation has finished for the previous person.

Regards

Owen.

Multiple Parameter UD4 / Access 2000 Database Search Tutorial:
www.tdsf.co.uk/tdsfdemo
Replied 14 Dec 2001 15:16:36
14 Dec 2001 15:16:36 Mitchel Tendler replied:
oeastwick,

thanks for the detailed reply, but regretfully you are way more advanced than I am and you totally lost me.

sorry,

Mitch

When in doubt...reboot!
Replied 14 Dec 2001 16:55:18
14 Dec 2001 16:55:18 Mitchel Tendler replied:
Well, this is what I came up with, it seems to work. oeastwick....thanks for pointing me in the right direction:




&lt;!--#include file="Connections/count.asp" --&gt;
&lt;%
set Recordset1 = Server.CreateObject("ADODB.Recordset"
Recordset1.ActiveConnection = MM_count_STRING
Recordset1.Source = "SELECT * FROM count"
Recordset1.CursorType = 0
Recordset1.CursorLocation = 2
Recordset1.LockType = 3
Recordset1.Open()
Recordset1_numRows = 0
%&gt;
&lt;%

set Command1 = Server.CreateObject("ADODB.Command"
Command1.ActiveConnection = MM_count_STRING
Command1.CommandText = "INSERT INTO count (pagename) VALUES ('surveys.asp') "
Command1.CommandType = 1
Command1.CommandTimeout = 0
Command1.Prepared = true
Command1.Execute()

%&gt;

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;
&lt;body bgcolor="#FFFFFF" text="#000000"&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;%
Recordset1.Close()
%&gt;

When in doubt...reboot!

Reply to this topic