SESSION variables in structure' with 'type' 'Array'
Hi I have a CFC/component by name "tsks_Session", that performs the session tasks. In this cfc/ init () function, created a structure that contains all the variables needed in the application.Some of the variables are array types.
<cfcomponent >
<cffunction name="init">
<cfargument name="structKey" >
<cflock timeout="35" >
<cfset SESSION = structNew() >
<cfset SESSION.bar_code = "" >
<cfset SESSION.price = "" >
<cfset SESSION.pub_date = "01/01/1900" >
<cfset SESSION.author = ArrayNew() >
<cfset SESSION.title = ArrayNew() >
<cfset SESSION.[bar_code_subj_pric] = structNew() > <!--- key = concatanation of
bar_code and price --->
<cfset SESSION.[bar_code_subj_pric].author = ArrayNew() >
<cfset SESSION.[bar_code_subj_pric].title = ArrayNew() >
</cflock>
</cffunction>
<!---getter--->
<cffunction name="getAuthor" returntype="array" access="public" output="false">
<cfscript>return SESSION.author; </cfscript>
</cffunction>
<!---setter:adding the Array/"author" to the structue/"SESSION.[bar_code_subj_pric]" --->
<cffunction name="setAuthor" retuntype="void" access="public" output="false">
<cfargument name="bar_code_subj_pric" type="string" required="true">
<cfargument name="author" type="array" required="true">
<cfset var q = "" >
<cfparam name="author" def开发者_运维技巧ault="" >
<cfloop index="i" from="1" to="arrayLen(SESSION.[bar_code_subj_pric].author)">
<cfset SESSION.author = ArrayAppend(SESSION.[bar_code_subj_pric].author,"#arguments.author#")>
</cfloop>
</cffunction>
<!---getter.title--->
<cffunction name="gettitle" returntype="array" access="public" output="false">
<cfscript>return SESSION.title; </cfscript>
</cffunction>
<!---setter:adding the Array/"title" to the structue/"SESSION.[bar_code_subj_pric]" --->
<cffunction name="settitle" retuntype="void" access="public" output="false">
<cfargument name="bar_code_subj_pric" type="string" required="true">
<cfargument name="title" type="array" required="true">
<cfset var q = "" >
<cfparam name="title" default="" >
<cfloop index="i" from="1" to="arrayLen(SESSION.[bar_code_subj_pric].title)">
<cfset SESSION.title = ArrayAppend(SESSION.[bar_code_subj_pric].title,"#arguments.title#")>
</cfloop>
</cffunction>
</cfcomponent>
1) on an display page, having instanitiated the cfc, i created a str called
<cfset str[expld133] =structnew()>
when i output the functions setAuthor("expld133",Kelly)/setTitle("expld133",33.22), i get "The value that i am not passing an array type".
Kindly tell me whats wrong?
2) can i create an structure called simply "SESSION" is it safe?
3)is there error in the way i am adding 2 different arrays(Author/Title) to the main structure "SESSION.[bar_code_subj_pric]"?
For question #1, I think that the line that is failing is probably one of these two:
setAuthor("expId133", Kelly);
setTitle("expId133", 33.22);
Both methods expect an array object as the 2nd argument; but you're passing a variable named "Kelly" in the first case, and a number in the second case. If "Kelly" is supposed to be the value, not a variable name, you would have to quote it ("Kelly"
not Kelly
), but that is still not an array. If you want it to be an array containing the 1 string "Kelly" then you would pass ["Kelly"]
assuming you're using CF8 or later.
For question #2, you can create a variable named "SESSION" (it will go into the component's VARIABLES scope), and it is "safe"... but I would strongly recommend against it. You're just asking for confusion. Also, it (the structure) won't be stored in the session scope (unless the component is stored there). Why not just use the session scope? Why create a new struct called "SESSION"?
For question #3, I'm not sure if that's valid syntax or not, but I would guess not. Try either of these:
SESSION[bar_code_subj_pric] = foo;
This will use bar_code_subj_pric
as a variable, and its value will be the structure key name. So if bar_code_subj_pric
evaluates to 4, the whole statement evaluates to: session.4 = foo;
On the other hand, this code:
SESSION.bar_code_subj_pric = foo;
//this is also equivalent to:
//SESSION["bar_code_subj_pric"] = foo;
... creates a key in the SESSION structure named "bar_code_subj_pric" with a value of the foo variable/object.
精彩评论