如何循环两个列表?

发布于 2025-01-02 18:02:03 字数 275 浏览 1 评论 0原文

我需要将两个单独列表的输出连接在一起以在 CFMAIL 中输出,我想知道解决此问题的最佳方法是什么。

我有两个表单字段:名字和姓氏,

每个字段最多有 5 个姓名。我需要循环遍历这些名称并连接名字和姓氏,然后将它们输出到无序列表。我很难想象实现这一目标的正确方法是什么。

有人可以建议 CFML 中的方法吗(我不太了解 CFSCRIPT)。

谢谢!

编辑:我应该补充一点,这两个字段将始终具有完全相同的条目数。感谢所有的回答——证明有很多方法可以剥猫的皮:)

I need to join the output of two separate lists together to output in a CFMAIL, and I'm wondering what the best way to approach this is.

I have two form fields: first_name and last_name

The fields have up to 5 names in each. I need to loop through those names and join the first and last names, then output them to unordered list. I am having trouble visualizing what the right approach to accomplish this is.

Can someone suggest a method in CFML (I don't know CFSCRIPT very well).

Thanks!

EDIT: I should have added that both fields will always have the exact same number of entries. Thanks to all that answered -- proof that there are a lot of ways to skin a cat :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梨涡少年 2025-01-09 18:02:03

我会做类似的事情,

<cfloop from="1" to="#ListLen(firstnames)#" index="i">
#ListGetAt(firstnames,i)# #ListGetAt(lastnames,i)#<br>
</cfloop>

如果这是一个包含 5000 个的列表,那么最好将其放入结构体或数组中,但对于约 5 个的列表,这应该足够了。

I would do something like

<cfloop from="1" to="#ListLen(firstnames)#" index="i">
#ListGetAt(firstnames,i)# #ListGetAt(lastnames,i)#<br>
</cfloop>

If this were a list of 5000 you would be better off putting it in a structure or an array, but for a list of ~5 this should be sufficient.

微暖i 2025-01-09 18:02:03

我认为这将是实现这一目标的最简单方法。

<!--- Create a names container --->
<cfset names = "<ul>">
<!--- Fill some dummy containers --->
<cfset first = "thomas,henry,philip,john,rony">
<cfset last = "smith,baker,crowe,ryan,jones">
<!--- Loop through the lists and append them to the container string --->
<cfloop index="name" to="#listLen(first)#" from="1">
 <cfset names &= "<li>" & ListGetAt(first,name) & " " & ListGetAt(last,name) & "</li>">
</cfloop>
<cfset names &= "</ul>">
<cfoutput>#names#</cfoutput>

I think this would be the easiest way to accomplish this.

<!--- Create a names container --->
<cfset names = "<ul>">
<!--- Fill some dummy containers --->
<cfset first = "thomas,henry,philip,john,rony">
<cfset last = "smith,baker,crowe,ryan,jones">
<!--- Loop through the lists and append them to the container string --->
<cfloop index="name" to="#listLen(first)#" from="1">
 <cfset names &= "<li>" & ListGetAt(first,name) & " " & ListGetAt(last,name) & "</li>">
</cfloop>
<cfset names &= "</ul>">
<cfoutput>#names#</cfoutput>
烟花易冷人易散 2025-01-09 18:02:03

我会添加一个检查以确保每个索引处都存在您的列表值,否则您会收到错误。我还会添加一个检查来循环遍历哪个列表更大,以便您获得所有值,以防万一有人没有在两个列表中输入正好 5:

<Cfset firstnames="Matt,Ian,Brandon,Sam,Tom">
<cfset lastnames="Jones,Smith,Weiss">

<!--- SEE WHICH LIST IS LONGER AND SET THAT AS THE ONE THAT WE WILL USE FOR THE LOOP --->
<cfif ListLen(firstnames) gte ListLen(lastnames)>
    <cfset primary=firstnames>
<cfelse>
    <cfset primary=lastnames>
</cfif>

<cfset myOutput="<ul>">
<cfloop from="1" to="#ListLen(primary)#" index="i">
    <Cfset myOutput &= "<li>">

    <cfif ListLen(firstnames) gte i>
        <cfset myOutput &= ListGetAt(firstnames,i)>
    </cfif>

    <cfif ListLen(lastnames) gte i>
        <cfset myOutput &= " " & ListGetAt(lastnames,i)>
    </cfif>

    <Cfset myOutput &= "</li>">
    </cfloop>
<Cfset myOutput &= "</ul>">


<cfoutput>#myOutput#</cfoutput>

I would add in a check to make sure that your list values exists at each index, otherwise you will get errors. I would also add in a check to loop through whichever list is greater so that you get all values just in case someone doesn't enter exactly 5 in both:

<Cfset firstnames="Matt,Ian,Brandon,Sam,Tom">
<cfset lastnames="Jones,Smith,Weiss">

<!--- SEE WHICH LIST IS LONGER AND SET THAT AS THE ONE THAT WE WILL USE FOR THE LOOP --->
<cfif ListLen(firstnames) gte ListLen(lastnames)>
    <cfset primary=firstnames>
<cfelse>
    <cfset primary=lastnames>
</cfif>

<cfset myOutput="<ul>">
<cfloop from="1" to="#ListLen(primary)#" index="i">
    <Cfset myOutput &= "<li>">

    <cfif ListLen(firstnames) gte i>
        <cfset myOutput &= ListGetAt(firstnames,i)>
    </cfif>

    <cfif ListLen(lastnames) gte i>
        <cfset myOutput &= " " & ListGetAt(lastnames,i)>
    </cfif>

    <Cfset myOutput &= "</li>">
    </cfloop>
<Cfset myOutput &= "</ul>">


<cfoutput>#myOutput#</cfoutput>
弃爱 2025-01-09 18:02:03

您可以将“list”属性与 CFLOOP 一起使用,尽管这意味着在输出中组合列表函数。下面是一个示例,说明如何完成此操作,并假设两个列表始终具有相同的长度。如果这些名称是由用户键入的,那么我可能会担心他们是否输入逗号,因为这会导致任何类型的循环失败。

<cfset lstFirstNames    = "John,Bob,Tom,Jeff" />
<cfset lstLastNames     = "Smith,Doe,Rodriguez,Horan" />

<cfloop list="#Variables.lstFirstNames#" index="FirstName" />
    #FirstName# #ListGetAt(Variables.LastNames, ListFind(Variables.lstFirstNames, FirstName))#<br />
</cfloop>

You could use the "list" attribute with CFLOOP although it means combining list functions within the output. Here is an example though of how it could be done and it makes the assumption the two lists will always have the same lengths. If these names are keyed in by users then I might be afraid of if they put in a comma since that would throw things off with any sort of looping.

<cfset lstFirstNames    = "John,Bob,Tom,Jeff" />
<cfset lstLastNames     = "Smith,Doe,Rodriguez,Horan" />

<cfloop list="#Variables.lstFirstNames#" index="FirstName" />
    #FirstName# #ListGetAt(Variables.LastNames, ListFind(Variables.lstFirstNames, FirstName))#<br />
</cfloop>
肩上的翅膀 2025-01-09 18:02:03

尝试:

<cfset lstFirstNames    = "John,Bob,Tom,Jeff" />
<cfset lstLastNames     = "Smith,Doe,Rodriguez,Horan" />

<cfloop list="#Variables.lstFirstNames#" index="FirstName">
    <cfoutput>#FirstName# #ListGetAt(Variables.lstLastNames, ListFind(Variables.lstFirstNames, FirstName))#</cfoutput><br />
</cfloop>

try:

<cfset lstFirstNames    = "John,Bob,Tom,Jeff" />
<cfset lstLastNames     = "Smith,Doe,Rodriguez,Horan" />

<cfloop list="#Variables.lstFirstNames#" index="FirstName">
    <cfoutput>#FirstName# #ListGetAt(Variables.lstLastNames, ListFind(Variables.lstFirstNames, FirstName))#</cfoutput><br />
</cfloop>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文