Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping through an array using ColdFusion

I have a shopping cart array, which has a variable to tell me if the product is an accessory or not, this will be either yes or no. I need to loop through the cart and find out the following:

  • If the cart contains accessories only; do whatever.
  • If the cart is products only; do whatever.
  • If the cart has products and accessories; do whatever.

I have been trying this:

<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
  <cfif session.mycart[i].accs EQ "yes">
    <cfset accPresent = "yes">
  </cfif>
  <cfif session.mycart[i].accs EQ "no">
    <cfset prodpresent = "yes">
  </cfif>
</cfloop>

<cfif accPresent EQ "yes" and prodPresent EQ "no">
  <cfset  bothPresent EQ "yes">
</cfif>

This falls down as accPresent is not found, this i think is due to the fact the loop goes through one at a time and the accs is not equal to yes once it find a non accessory product. What's the best way to achieve what I'm trying to do?

like image 255
Jason Congerton Avatar asked Feb 12 '12 21:02

Jason Congerton


1 Answers

Do this

<cfset accPresent = "no" />
<cfset prodPresent = "no" />
<cfloop index="i" from="1" to="#arrayLen(session.mycart)#">
    <cfif session.mycart[i].accs EQ "yes">
        <cfset accPresent = "yes">
    </cfif>
    <cfif session.mycart[i].accs EQ "no">
        <cfset prodpresent = "yes">
    </cfif>
</cfloop>

<cfif accPresent EQ "yes" and prodPresent EQ "no">
    <cfset  bothPresent EQ "yes">
</cfif>
like image 105
Dale Fraser Avatar answered Oct 02 '22 15:10

Dale Fraser