Just a thought but are you calling init() on cart each and every time you
create it eg., 
<cfset variables.cart = createobject("component","cart").init() />

If you miss init() you wont have access to instance scope. The error will
throw on the call to findItem as instance.arritems in that method wont
exist. 

Angus

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Sameer Kekade
Sent: Thursday, 11 November 2004 9:32 AM
To: [EMAIL PROTECTED]
Subject: [CFCDev] Shopping cart cfc

Hey All,

Does anyone have a shopping cart cfc that I can startup with. I have
this shopping cart.cfc and it randomly throws an error when I try to
insert an item into my ordercart. It shows an error on this line <cfset
variables.indexItem = findItem(trim(arguments.ean),
trim(arguments.supplier_id))>

Am I doing anything wrong??


<cfcomponent>
        <cfproperty name="arrItems" type="array">
        <cfproperty name="timestamp" type="date">
                
        <cffunction name="init" access="public" output="false"
returntype="struct" hint="Initialize the object">
                <cfscript>
                instance = structNew();
                instance.arrItems = arrayNew(1);
                instance.timestamp = Now();
                </cfscript>
                <cfreturn this>
        </cffunction>
        
        <cffunction name="insertItem" access="public" output="false"
returntype="string">
                <cfargument name="supplier_id" type="string">
                <cfargument name="ean" type="string">
                <cfargument name="qty" type="string">
                <cfargument name="price" type="string">
                
                                
                <cfset variables.flgAddRecord = true><!--- default to
true (means add the record into the cart) --->
                
                <cfset variables.indexItem =
findItem(trim(arguments.ean), trim(arguments.supplier_id))>

                <cfif variables.indexItem NEQ 0>
                                <cfset
instance.arrItems[variables.indexItem].qty =
instance.arrItems[variables.indexItem].qty + arguments.qty>
                                <cfset
instance.arrItems[variables.indexItem].extprice =
instance.arrItems[variables.indexItem].extprice +
(arguments.price*arguments.qty)>                                
                                <cfset variables.flgAddRecord = false>
<!--- false (since its been consolidated, do not add the record into the
cart) --->
                </cfif>
                
                <!--- The cart array was traversed and the item doesnot
exist in the ordercart hence append the item into the cart --->
                <cfif  variables.flgAddRecord EQ true>
                        <cfset Item = structNew()>
                        <cfset Item.supplier_id =
trim(arguments.supplier_id)>
                        <cfset Item.ean = trim(arguments.ean)>
                        <cfset Item.qty = arguments.qty>
                        <cfset Item.price = arguments.price>
                        <cfset Item.extprice =
(arguments.price*arguments.qty)>
        
                        <cfset temp =
ArrayAppend(instance.arrItems,Item)>
                </cfif><!---flgAddRecord EQ true --->

                <cfreturn flgAddRecord>
        </cffunction>
        
        
        <cffunction name="get" access="public" output="false"
returntype="array" hint="Returns the current Order Cart.">
                <cfif not isDefined("instance")>
                        <!--- Entity has not been inited yet --->
                        <cfset this.init()>
                </cfif>
                <cfreturn Instance.arrItems>
        </cffunction>

        
        <cffunction name="findItem" access="public" output="false"
returntype="numeric">
                <cfargument name="ean" type="string">
                <cfargument name="supplier_id" type="string">
                <cfset ii=0>
                <cfset arrTemp = instance.arrItems>
                <cftry>
                        <cfscript>
                        //a variable for looping
                                //loop through the array, looking for
the value
                                for(ii = 1; ii LTE arrayLen(arrTemp); ii
= ii + 1){
                                        //if this is the value, return
the index
                                        if(
(compare(arrTemp[ii].ean,arguments.ean) EQ 0) and
(compare(arrTemp[ii].supplier_id,arguments.supplier_id) EQ 0))
                                                return ii;
                                }
                                //if we've gotten this far, it means the
value was not found, so return 0
                                return 0;
                        </cfscript>
                        <cfcatch type="any">
                                <!--- do nothing --->
                                <cfreturn 0>
                        </cfcatch>
                </cftry>
                
        </cffunction>
        
        
        
        <cffunction name="resetCart" access="public" output="false"
returntype="boolean">
                <!--- Delete the cart data --->
                <cfset temp = ArrayClear(instance.arrItems)>
                <cfreturn ArrayIsEmpty(instance.arrItems)>
        </cffunction>
        
</cfcomponent>

Warm Regards,
Sameer S. Kekade.


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
[EMAIL PROTECTED]

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at 
[EMAIL PROTECTED]

Reply via email to