Skip to main content
Inspiring
March 11, 2009
Question

Turning queries into session variables

  • March 11, 2009
  • 1 reply
  • 2257 views
Hello;
I am trying to learn about session variables for more stability in an application I am writting. I am still not sure of how this works and was hoping someone can show me so I can get the idea.

I want to make the part of my application that updates info into sessions. I want the sessions to control the usage of the section. So if user a is logged in, no one else can make changes to the area until they log out. Somethign like that, or they can log in, but can't make changes if another user is.

I realize some of this is controled by islogged in and the session started by the logged in user. How do I make my application into a session managed app?

Here is some code, just some of the code in my app, I will show you what I am doing, I don't think it is right.

this is the start page:

<cfset preappendURL = "../">
<cfset pageType = "admin">
<cfquery name="catMan" datasource="#APPLICATION.dataSource#">
SELECT Categories.Name AS ViewField1, Categories.CategoryID
FROM Categories
</cfquery>
<cfset rowsPerPage = 6>
<cfparam name="URL.startRow" default="1" type="numeric">
<cfset totalRows = catMan.recordCount>
<cfset endRow = min(URL.startRow + rowsPerPage - 1, totalRows)>
<cfset startRowNext = endRow + 1>
<cfset startRowBack = URL.startRow - rowsPerPage>
<head>
</head>
<body>
<cfoutput>
Displaying <b>#URL.startRow#</b> to <b>#endRow#</b> of <b>#totalRows#</b> Records.
<cfif startRowBack GT 0><a href="#CGI.script_name#?startRow=#startRowBack#" class="nav">&lt; Previous Records</a></cfif>

<cfif startRowNext lte totalRows><a href="#CGI.script_name#?startRow=#startRowNext#" class="nav">Next Records &gt; </a>
</cfif>
</cfoutput>'

<cfloop query="catMan" startRow="#URL.startRow#" endrow="#endRow#">
<cfset class = iif(catMan.currentRow mod 2 eq 0, " 'DataA' ", " 'DataB' ")>
<cfoutput>
#ViewField1#
<a href="project-manager.cfm?CategoryID=#CategoryID#" class="nav">Edit Projects</a>
<a href="projectCat-edit.cfm?CategoryID=#CategoryID#" class="nav">Edit Category</a>
<form action="ProjectCat-Action.cfm" method="post">
<input type="hidden" name="ID" value="#CategoryID#">
<input type="submit" name="cat_Delete" class="formButtons" onClick="return confirmDelete('ID','#ViewField1#')" value="Delete"></form>
</cfoutput></cfloop>

<cfoutput>
<cfset thisPage = 1>
<cfloop from="1" to="#totalRows#" step="#rowsPerPage#" index="pageRow">
<cfset isCurrentPage = (pageRow gte URL.startRow) and (pageRow lte endRow)>
<cfif isCurrentPage>
<cfoutput><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b>#thisPage#</b></font></cfoutput><cfelse><cfoutput>
<a href="#CGI.script_name#?startRow=#pageRow#" class="nav">#thisPage#</a></cfoutput></cfif>
<cfset thisPage = thisPage + 1>
</cfloop>

<cfif startRowBack GT 0><a href="#CGI.script_name#?startRow=#startRowBack#" class="nav">&lt; Previous Records</a></cfif>

<cfif startRowNext lte totalRows><a href="#CGI.script_name#?startRow=#startRowNext#" class="nav">Next Records &gt; </a>
</cfif>
</cfoutput>
</body>

There is another page that goes with this. It is the edit page. But I don't know if we can start here to create this part into sessions.

Here is page 2:

<cfset preappendURL = "../">
<cfset pageType = "admin">
<cfparam name="url.id" type="integer" default="0">
<cfparam name="variables.CategoryID" type="integer" default="#url.id#">
<cfparam name="variables.Name" default="">
<cfparam name="variables.Description" default="">
<cfparam name="variables.MYFile" default="">


<cfif url.id GT 0>
<cfquery name="categRec" dataSource="#APPLICATION.dataSource#">
SELECT Name, Description, MYFile, CategoryID
FROM Categories
WHERE CategoryID = <cfqueryparam value="#URL.ID#" cfsqltype="cf_sql_integer">
ORDER BY Name
</cfquery>

<!--- if the record was found, store the values --->
<cfif categRec.RecordCount EQ 1>
<cfset variables.CategoryID = categRec.CategoryID>
<cfset variables.Name = categRec.Name>
<cfset variables.Description = categRec.Description>
<cfset variables.MYFile = categRec.MYFile>
</cfif>
</cfif>
<head>
</head>
<body>
<cfoutput>
<form action="ProjectCat-Action.cfm" method="post" name="content" id="content"
enctype="multipart/form-data">
<input type="hidden" name="ID" value="#variables.CategoryID#">
<input type="hidden" name="oldimage" value="#variables.MYFile#">

<input type="text" name="Name" class="textInputs"
value="#variables.Name#" maxLength="510">

<cfif len(trim(variables.MYFile)) GT 0>
<b>Image in use:</b>#variables.MYFile#</cfif>

<input name="MYFile" type="file" id="MYFile">

<input name="Description" type="text" class="textInputs"
value="#variables.Description#" size="50" maxLength="510">
</cfoutput>
<input type="submit" class="formButtons" name="cat_OK" onclick=";DisableButton(this);" value=" OK ">
<input type="submit" class="formButtons" name="cat_Cancel" value="Cancel">
</form>

How would I make this into session managed variables? That is where I am confused. I have session variables enabled in my application.cfc file. Now I want to manage them better.

Can anyone help show me how to do this? Or point me to a web site that explains what I am trying to do?

Thank you.

CFmonger
This topic has been closed for replies.

1 reply

Inspiring
March 11, 2009
First of all, to make a query a session variable - just put it there.

<cfquery name="session.contentMAN"...>
...
</cfquery>

<cfoutput query="session.contentMAN">
...
</cfoutput>

Now I need to tell you this will not help you in your stated
requirement. Putting data into the session scope will not make your
application 'session' aware. Data in the session scope is global only
to one, individual user. No other user can see what is in any other
user's session scope. [Unless you do some strange stuff with
undocumentated and unsupported ColdFusion java objects]

To do that you need a scope that is global to all users of the
application and that is the 'application' scope. Luckily this is just
as easy to use.

<cfquery name="application.contentMAN"...>
...
</cfquery>

<cfoutput query="application.contentMAN">
...
</cfoutput>

I'll leave it to you to figure out all the logic for what data gets
stored in what scopes and what decisions your code makes based on this data.
CFmongerAuthor
Inspiring
March 11, 2009
ok, that makes sense. so if I put my query into the application scope, I will be able to manage my users easier? Correct?

So once the query is in the application scope, I put the rest in the same scope? or not?

Like this:

<cfparam name="url.id" type="integer" default="0">
<cfparam name="application.CategoryID" type="integer" default="#url.id#">
<cfparam name="application.Name" default="">
<cfparam name="application.Description" default="">
<cfparam name="application.MYFile" default="">

<!--- if the record was found, store the values --->
<cfif application.categRec.RecordCount EQ 1>
<cfset application.CategoryID = categRec.CategoryID>
<cfset application.Name = categRec.Name>
<cfset application.Description = categRec.Description>
<cfset application.MYFile = categRec.MYFile>
</cfif>
</cfif>

and then this?


<cfoutput>
<form action="ProjectCat-Action.cfm" method="post" name="content" id="content"
enctype="multipart/form-data">
<input type="hidden" name="ID" value="#application.CategoryID#">
<input type="hidden" name="oldimage" value="#application.MYFile#">

and so on with the rest of this page.

But for the action part, do I make all those into the application variables as well?

<cfquery datasource="#APPLICATION.dataSource#">
UPDATE Categories
SET
<cfif fileuploaded is true>
Categories.MYFile=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#application.uploadedfile#">,
</cfif>
Categories.Name=<cfqueryparam cfsqltype="cf_sql_varchar"
value="#form.application.Name#">,
Categories.Description=<cfqueryparam cfsqltype="cf_sql_longvarchar"
value="#form.application.Description#">
WHERE CategoryID = <cfqueryparam value="#form.application.ID#" cfsqlType="CF_SQL_INTEGER">
</cfquery>

Is that how it is done?
and thank you. I had an idea, but I am trying to decide my best way to approach this.