问题
I'm working on a Coldfusion project and I seem to be stuck. I am a newbie so I hope I don't get too much slack. The purpose of my project is to create a password list by using nested loops. I am to design a template that combines all words in the list "cold, fusion, dynamic" with all words on the list "bert, ernie, oscar" to produce a bulleted list of vaild passwords. This template should process two URL prarameters named List1 and List2. I must use two list loops nested within each other to produce all possible combinations of words. (For example "coldbert", "coldernie", "coldoscar", "fusionbert" and so on..)
This is what I have so far:
<cfinclude template="header.cfm">
<body>
<h2>Loop List</h2>
<cfhttp url="looplist.cfm?List1=cold,fusion,dynamic&List2=bert,ernie,oscar" method="get">
<CFLOOP LIST="#URL.List1#"
INDEX="List1">
<UL><CFOUTPUT>#List1#</CFOUTPUT></UL><br>
</CFLOOP>
<cfinclude template="footer.cfm">
I want to ensure that I'm going in the right direction here. Thanks guys for any assistance.
回答1:
Unless you're calling a page that doesn't exist on your site, I do not see a need to do a http call. You could just create a function in the template (although I'd prefer it to be in a separate cfc) and call that to get your password combos. Something like ...
<cffunction name="getPasswordCombos" returntype="string">
<cfargument name="list1" type="string" required="true" />
<cfargument name="list2" type="string" required="true" />
<cfset var passwordCombos = "" />
<cfset var i = "" />
<cfset var j = "" />
<!--- your combo generation logic might look something like --->
<cfloop list="#arguments.list1#" index="i">
<cfloop list="#arguments.list2#" index="j">
.....
<!--- set passwordCombos logic here --->
.....
</cfloop>
</cfloop>
<cfreturn passwordCombos />
</cffunction>
Then,
<cfset passwordCombos = getPasswordCombos("cold,fusion,dynamic", "bert,ernie,oscar") />
Then loop over the "passwordCombos"
<ul>
<cfloop list="#passwordCombos#" index="i">
<li>#i#</li>
</cfloop>
</ul>
Also, if you HAVE to user CFHTTP, use cfhttpparam to pass in arguments. It's much cleaner.
<cfhttp result="result" url="looplist.cfm" method="GET">
<cfhttpparam name="list1" type="url" value="cold,fusion,dynamic">
<cfhttpparam name="list2" type="url" value="bert,ernie,oscar">
</cfhttp>
来源:https://stackoverflow.com/questions/42614417/creating-combinations-using-cf-list-loops