Generating unique combinations


i building product configuration module part of custom
e-commerce site , trying wrap brain around teasing little
problem. when product added system, system
administrator able select options available
product. these options supplied strings of numbers in form
scope (by selecting checkboxes). each option have it's own set
of numbers. example shirt may available in 3 sizes (s m l)
and 2 colours (blue , red) passed through form
like so: 1,2,3 sizes , 55,66 colours. far, good.

what need able generate unique combinations of
options each product selecting 1 number each string ,
generating unique combination. these values stored in
new single dimension array. able work out number of unique
combinations (in above example 6), having
difficulties in generating unique combos. far have
following code:

<!--- dummy values testing (this represents form values
would passed in)--->
<cfset myarray = arraynew(1)>
<!--- represents colours --->
<cfset myarray[1] = '22'>
<!--- represents sizes --->
<cfset myarray[2] = '32,33,34,35,36,37'>
<!--- represents cut (regular/long etc) --->
<cfset myarray[3] = '6,7'>

<!--- work out number of unique combinations --->
<!--- start out @ 0 --->
<cfset numofcombos = 0>
<!--- how many sets of options there... --->
<cfset arrlen = arraylen(myarray)>
<!--- loop through sets of options --->
<cfloop from="1" to="#arrlen#" index="j">
<cfif numofcombos 0>
<cfset numofcombos = listlen(myarray[j],',')>
<cfelse>
<cfset numofcombos = numofcombos * listlen(myarray[j],',')>
</cfif>
</cfloop>

<!--- new array hold unique combos --->
<cfset newarray = arraynew(1)>
<!--- create number of required array elements --->
<cfloop from="1" to="#numofcombos#" index="i">
<cfset newarray = ''>
</cfloop>

<!--- populate new array 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[k] = listappend(newarray[k],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>
<!--- should unique combos --->
<cfdump var="#newarray#" label="newarray - unique combos">

the above code correctly generates required number of combinations,
however, not of them unique. please can point me in
right direction?

thanks,

richard.

that's tough 1 folks. if job, use cold fusion query rather arrays. easier figure out doing if had column names instead of column numbers.

using data of 3 sizes (s,m.l), 2 colours (red, blue), , 2 cuts (reg, wide), try this.

build query 3 columns, size, colour, , cut. add 12 empty rows ( 3 * 2 * 2).

then populate columns 1 one. size column gets s in rows 1, 4, 7, , 10, m in rows, 2, 5, 8, , 11, , l in 3, 6, 9, , 12. same each column , have unique combos. plus easy loop through query whatever next step is.


More discussions in Advanced Techniques


adobe

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial