I use the following code to show how many user are online And it is working fine. But i have added a little a second select query and update query to upadte another feild that should keep track of how many users were online at one time. <cfquery datasource="#application.datasource#" name="count"> SELECT MOST_ONLINE FROM ONLINE_INFO WHERE ID = 'ABA6C547...' </cfquery> <cfquery datasource="#application.datasource#" name="online"> SELECT ONLINE FROM USER_LOGIN WHERE ONLINE = '1' </cfquery> <cfparam name="onlinenow" default=""> <cfset onlinenow eq count.most_online> <cfif online.recordcount gt onlinenow> <cfquery datasource="#application.datasource#" name="online_info"> UPDATE ONLINE_INFO SET MOST_ONLINE = '#online.recordcount#' WHERE ID = 'ABA6C547...' </cfquery> </cfif> <!--- Heres where we show how many online and how many ever online at one time ---> <cfif online.online gte 1> <img src="images/online.gif" alt="Online"/> </cfif> <cfoutput> #online.recordcount# Online | Most online at one time: #count.most_online#</cfoutput> | Login, Hangout! Code (markup): But instead of the math working up in the cfif statement it will run the update query no matter what. So basically the most online number will change to the current number online every time. Thanks a bunch for you great Ideas!
Where does the onlinenow value come from? Dump the variable scope and check the values of of online.recordcount and onlinenow to see why the update is excecuting every time <cfdump var="#variables#"> Code (markup): Also, if you only need a count of records, it would be a more efficient to do this. <cfquery datasource="#application.datasource#" name="online"> SELECT COUNT(*) AS NumberOnline FROM USER_LOGIN WHERE ONLINE = '1' </cfquery> Code (markup): Then use the #online.NumberOnline# value (not recordCount)
The onlinenow variable is just arbitrary . I thought maybe I needed to create an arbitrary variable in order to set it eq to count.most_online. Thanks for the tip. I'll try these couple of changes and see what i come up with.
Oh! Now I understand what the code was trying to do here. <cfset onlinenow eq count.most_online> ... should be <cfset onlinenow = count.most_online> Yeah, you're right. You can just use count.most_online.
Just by changing the eq to = made all the difference in the world. But as you said before, Is it still more efficient to use? <cfquery datasource="#application.datasource#" name="online"> SELECT COUNT(*) AS NumberOnline FROM USER_LOGIN WHERE ONLINE = '1' </cfquery> [/code Code (markup):
Yes, it is. Let's say your site has become very popular and right now there are 1000 people online. The first query will retrieve 1000 records. The second query will retrieve only (1). A single record containing a total number (ie 1000).
This work perfect, I was having a problem because I forgot to change the cfif statement where i display the image and the number of online viewers. I needed to change that variable to #online.NumberOnline# from #online.recordcount# too But I thought the problem was that I needed to name the column to count. Turns out that I didn't but naming th column doesn't hurt. Thanks again