views:

58

answers:

2

I need to create a shopping cart there are many variables like Shirts,CompanyText,Desiner_Names,Company,Cons_Name,DSNR_Cert_Number,DSNR_Cert_Issued_Date etc. Here "Shirts,CompanyText,Desiner_Names" are all Array type variables where "Shirts" is the main Array.

So i need to basically work on Array that has inside it 2 different Arrays along with other variables two Arrays inside an Array. The code is

 <!--- create a MAIN array  --->
<cfset Shirts= ArrayNew(1) />
<!--- "Cons_Code" is the key value taken from "Cons_name" and "Company_Text_Code" 
    Also every time a user selects a new "Shirt", a new key "Cons_Code" is created   --->
<cfset Cons_Code = 2 >
<!--- create a structure  --->
<cfset stShirts[Cons_Code] = StructNew() />
<!--- assign values to our structure --->
<cfset stShirts[Cons_Code].Company_Text_Code = "" /><!---
<cfset stShirts[Cons_Code].CompanyText = "333" />--->
<cfset stShirts[Cons_Code].Desiner_Names = ArrayNew(1) />
<cfset stShirts[Cons_Code].Desiner_Names[1] = "PARIS" />
<cfset stShirts[Cons_Code].Desiner_Names[2] = "LONDON" />
<cfset stShirts[Cons_Code].Desiner_Names[3] = "MILAN" />
<cfset stShirts[Cons_Code].CompanyText = ArrayNew(1) />
<cfset stShirts[Cons_Code].CompanyText[1] = "perryellis" />
<cfset stShirts[Cons_Code].CompanyText[2] = "arrow" />
<cfset stShirts[Cons_Code].CompanyText[3] = "polo" />
<cfset stShirts[Cons_Code].Cons_Name = "" />
<cfset stShirts[Cons_Code].DSNR_Cert_Number = "" />
<cfset stShirts[Cons_Code].DSNR_Cert_Issued_Date = "" />
<cfset ArrayAppend( Shirts, StructCopy( stShirts[Cons_Code] ) ) />
<!--- store structure in our array --->

<cfoutput>
    <cfloop from="1" to="#ArrayLen(Shirts)#" index="row">
  Row #row#: 
        #Shirts[row].CompanyText[1]#, 
        #Shirts[row].Desiner_Names[2]#,    
        #Shirts[row].Cons_Name#<br />
        #Shirts[row].DSNR_Cert_Issued_Date#<br />
   </cfloop>
</cfoutput>

Sometimes I am getting the error

"You have attempted to dereference a scalar variable "

Also am i doing the right thing by having 2 arrays inside an one by one Aray/Shirts? This "Shirts" contains at least 150 other different vairables, that go into session. Help pleez. xain hu

+2  A: 

You have commented out

<cfset stShirts[Cons_Code].CompanyText = "333" />

I suspect you still have something like this in your code somewhere, though. Basically, your error tells you that you are using array or struct notation to try to access a variable, but that variable isn't a struct or array.

Ben Doom
still the same error
Xian
"Still the same error" after what? Sergii is right -- you need to provide more information before we can help you. Right now, we have very little idea of what is actually going on.
Ben Doom
+1  A: 

You can trouble shoot the cart with cfdump. Break up each phase that will make the cart, and test if it does what it is supposed to do. Once it has done what it needs to do, then you can put it together.

When you run a cfdump after this line,

<!--- store structure in our array --->
<cfdump var="#stshirts#">

you will note that the first index in the array is undefined, and the second will have your structure along with the sub data.

Wouldn't you rather have the array dynamically populate itself so that you do not have any empty index(s)? You also will not need to worry about generating your own unique index, just feed the structure with what ever data you got, then append it to the array.

<cfoutput>
 #ArrayAppend(ArrayName, "#StrucureName#")#
</cfoutput>

With the above, you will be able to add the structure to your array, so long as the array has been defined before this code is executed. The index in the array will be dynamically added or removed accordingly, leaving a sequential index, without any gaps.

If you want to get anything from your structure,

<cfloop index="i" from="1" to="#ArrayLen(ArrayName)#">
 <cfoutput>
   #ArrayName[i].NameOfKeyInStructure#
 </cfoutput>
</cfloop>
Nich