Link to home
Start Free TrialLog in
Avatar of tanc02
tanc02

asked on

a cold fusion counter ?

How to write a cold fusion counter script ?
Avatar of meverest
meverest
Flag of Australia image

<cfset application.thispagecount = application.thispagecount+1>

<cfoutput>
Now this page has been accessed #application.pagecount# times!
</cfoutput>

make sure that you have application variable support enabled.
Avatar of tanc02
tanc02

ASKER

How do I know whether my cold fusion can have
application variable support enabled.

Can you tell me how to set it step by step ?
I have Cold Fusion 4.5 eval version
Avatar of tanc02

ASKER

In PERL, I have to lock and unlock the counter
file. How about in Cold Fusion ?
from memory, it is enabled in the cfapplication tag.

check the cf docs, if you can't find it, remind me on monday and i will check for you.

cheers.
Avatar of tanc02

ASKER

Can you tell me how ? I couldn't find  the info thanks !
There is a small counter solution that makes use of a number field in a database.

It's like you have your datasource, say an MS Access file, called "myDSN" in which you have a table called "counters" and a field called "myMainCounter", which is a number field.

Then you will do it like this:

<cfquery name="visitorCount" datasource="myDSN">
 select myMainCounter from counters
</cfquery>

Now to display this value, you will do it like this:

<cfoutput>
You are visitor #int(visitorCount.myMainCounter)#
</cfoutput>

Now you will have to increment this value, do like so:

<cfquery name="visitorCount" datasource="myDSN">
 update counters set myMainCounter = #IncrementValue(int(visitorCount.myMainCounter))#
</cfquery>
Oooops!! The update query should be like this:

<cfquery name="updateVisitorCount" datasource="myDSN">
 update counters set myMainCounter = #IncrementValue(int(visitorCount.myMainCounter))#
</cfquery>

I had forgotten to change the name of the query! My apologies!

(the bad side of Ctrl-C/Ctrl-V!!) :))
If you can't get these to work and want a solution written for you I will do it.

Nathan Stanford
Mr. ColdFusion

check the docs for <cfapplication> for additional detail...

<CFAPPLICATION NAME="Name"
    CLIENTMANAGEMENT="Yes/No"
    CLIENTSTORAGE="Storage Type"
    SETCLIENTCOOKIES="Yes/No"
    SESSIONMANAGEMENT="Yes/No"
    SESSIONTIMEOUT=#CreateTimeSpan(days, hours,
      minutes, seconds)#
    APPLICATIONTIMEOUT=#CreateTimeSpan(days, hours,
      minutes, seconds)#>

the attribute you are interested in is:

APPLICATIONTIMEOUT
Optional. Enter the CreateTimeSpan function and the values you want in days, hours, minutes, and seconds, separated by commas to specify the lifespan of any application variables that are set. The default value is specified in the Variables page of the ColdFusion Administrator.

in CF administrator (usually http://127.0.0.1/CFIDE/Administrator/index.cfm) choose 'variables' from the menu and make sure application variables is checked.

for example:

<cfapplication name="MyApp" APPLICATIONTIMEOUT=#CreateTimeSpan(365, 0, 0, 0)#>

this will allow the application variable to remain static for 1 year with no activity.

cheers.



Are you still looking for an answer?  tanc02 if not award your points to one of the above???
Avatar of tanc02

ASKER

Adjusted points from 5 to 20
Avatar of tanc02

ASKER

nathans :

May I see you solution and can you tell me how to lock/unlock a file ?

Points increase to 20
Hi,

For unlock what ?
an access file ? (.lbd)
if yes, try :
#cfusion_dbconnections_flush()#

Phil
.eh? wazzat, samphi?
Avatar of tanc02

ASKER

to lock mdb file
<!---
No need to tell it which page it is coming from it will take care of that for you.

Your Database Needs of you can change the names to fit your needs.

datasource: counter
Table: counter
Cols: PageName
      hits

Nathan Stanford
Mr. Coldfusion
Check Out my ColdFusion Tips Plus e-ZINE
http://www.nsnd.com/cftips
 --->

<cfset PageName = #cf_template_path#>
<cftransaction>

<cfquery name="CountHits" datasource="counter">
      SELECT * FROM counter
      WHERE PageName  = '#PageName#'
</cfquery>

<CFIF #CountHits.recordcount# is 0>
 <CFQUERY name="makePageName" datasource="counter">
  INSERT INTO counter (`PageName`, `hits`)
  VALUES ('#PageName#', 1)
</cfquery>
1
<CFELSE>
<cfoutput>
 #CountHits.hits#
</cfoutput>
<CFSET newhits = (CountHits.hits + 1) > 
 <CFQUERY name="updatehits" datasource="counter">
  UPDATE counter
  SET hits = #newhits#
  WHERE PageName = '#PageName#'
 </cfquery>
</CFIF>
</cftransaction>

Ey, on that note Nathans -- I notice in your INSERT query:

<CFQUERY name="makePageName" datasource="counter">
  INSERT INTO counter (`PageName`, `hits`)
  VALUES ('#PageName#', 1)
</cfquery>

...you have backticks/backquotes in the table field names as in `PageName`. What do SQL specs. say regarding the use of these in INSERT/UPDATE queries? Doesn't this have any side effects elsewhere as in some DBMs?
Sorry, take out the Backticks... it was working for me so I forgot they were there.

Thanks for letting me know.

By the way I am planning to write a ColdFusion Tips Plus (javascript as well) book if anyone is interested email me and let me know at
nathan@nsnd.com

Nathan Stanford
Mr. ColdFusion
http://www.nsnd.com/cftips
ASKER CERTIFIED SOLUTION
Avatar of Nathan Stanford Sr
Nathan Stanford Sr
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial