如何循环两个列表?
我需要将两个单独列表的输出连接在一起以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我会做类似的事情,
如果这是一个包含 5000 个的列表,那么最好将其放入结构体或数组中,但对于约 5 个的列表,这应该足够了。
I would do something like
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 think this would be the easiest way to accomplish this.
我会添加一个检查以确保每个索引处都存在您的列表值,否则您会收到错误。我还会添加一个检查来循环遍历哪个列表更大,以便您获得所有值,以防万一有人没有在两个列表中输入正好 5:
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:
您可以将“list”属性与 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.
尝试:
try: