I am building a product configuration module as part of a
custom
e-commerce site and I am trying to wrap my brain around a
teasing little
problem. When a product is added into the system, the system
administrator is able to select what options are available
for that
product. These options are supplied as strings of numbers in
the form
scope (by selecting from checkboxes). Each option will have
it's own set
of numbers. For example a shirt may be available in three
sizes (S M L)
and two colours (blue and red) which would be passed through
to the form
like so: 1,2,3 for the sizes and 55,66 for the colours. So
far, so good.
What I need to be able to do is generate the unique
combinations of
options for each product by selecting one number from each
string and
generating a unique combination. These values are to be then
stored in a
new single dimension array. I am able to work out the number
of unique
combinations (in the above example this would be 6), however
I am having
difficulties in generating the unique combos. So far I have
the
following code:
<!--- Dummy values for testing (this represents the form
values that
would be passed in)--->
<cfset myArray = arraynew(1)>
<!--- This represents colours --->
<cfset myArray[1] = '22'>
<!--- This represents sizes --->
<cfset myArray[2] = '32,33,34,35,36,37'>
<!--- This represents cut (regular/long etc) --->
<cfset myArray[3] = '6,7'>
<!--- Work out the number of unique combinations --->
<!--- Start out at zero --->
<cfset numOfCombos = 0>
<!--- How many sets of options are there... --->
<cfset arrLen = arraylen(myArray)>
<!--- Loop through the sets of options --->
<cfloop from="1" to="#arrLen#" index="j">
<cfif numOfCombos is 0>
<cfset numOfCombos = listlen(myArray,',')>
<cfelse>
<cfset numOfCombos = numOfCombos *
listlen(myArray,',')>
</cfif>
</cfloop>
<!--- New Array to hold unique combos --->
<cfset newArray = arraynew(1)>
<!--- Create the number of required array elements --->
<cfloop from="1" to="#numOfCombos#" index="i">
<cfset newArray
= ''>
</cfloop>
<!--- Populate new Array with unique combos --->
<cfloop from="1" to="#arrLen#" index="i">
<cfset numOfEach =
numOfCombos/(listlen(myArray,','))>
<cfset stepNum = numOfCombos/numOfEach>
<cfloop from="1" to="#listlen(myArray
,',')#" index="x">
<cfset thisValue = listgetat(myArray,x,',')>
<cfloop from="#x#" to="#numOfCombos#" step="#stepNum#"
index="k">
<cfset newArray =
listappend(newArray,thisValue,',')>
</cfloop>
</cfloop>
</cfloop>
<!--- Output values --->
<!--- Original data --->
<cfdump var="#myArray#" label="myArray - Original
Data">
<!--- Number of unique combos --->
<p>Number Of Unique Combos:
<cfoutput>#numOfCombos#</cfoutput></p>
<!--- This should be the unique combos --->
<cfdump var="#newArray#" label="newArray - Unique
Combos">
The above code correctly generates the required number of
combinations,
however, not all of them are unique. Please can someone point
me in the
right direction?
Thanks,
Richard.