Coldfusion:插入父/子动态表单字段

发布于 2024-11-19 21:53:39 字数 401 浏览 2 评论 0原文

需要一些帮助思考如何解决这个问题。

我有一个动态表单,允许用户创建问题。他们可以提出一对多的问题。

然后,这些问题可以有一对多与之相关的答案。

UI由jquery驱动就完成了。字段名称发布到处理页面,如下所示:

问题如下所示: Question1、Question2、Question3 等。

答案如下所示: Question1-Answer1、Question1-Answer2、Question2-Answer1 等

我知道我需要循环问题,插入、获取新 ID,并将其应用于答案,以在我的数据库中建立问题和答案之间的关系。

至于如何从技术上解决这个问题,我的第一个猜测是构建一个问题数组,数组的第二列是答案数组。然后我就循环遍历数组。

帮助? :) 谢谢!

need some help thinking through how to approach this.

I have a dynamic form that allows the user to create questions. They can have one to many questions.

The questions can then in turn have one to many answers associated with them.

The UI is driven by jquery and is done. The field names post to the processing page like so:

Questions look like: Question1, Question2, Question3, etc.

Answers look like: Question1-Answer1, Question1-Answer2, Question2-Answer1, etc

I know I need to loop over the questions, insert, grab the new ID, and apply it to the answer to build the relationship between question and answer in my database.

As for how to technically approach this, my first guess is to build an array of questions with the second column of the array being an array of answers. I'd then just loop over the array.

Help? :) Thanks!

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

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

发布评论

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

评论(3

软糯酥胸 2024-11-26 21:53:39

不要将所有问题和答案插入大量循环语句中。逻辑,在用户添加它们时插入它们。即听起来您需要一个“添加另一个问题”按钮,使用 javascript[Ajax?] 添加另一个问题字段 - 使用它也可以将问题插入数据库中。

Instead of inserting all your questions and answers in a bulk statement with lots of looping & logic, insert them as the user adds them. i.e. sounds like you will need an "add another question" button, using javascript[Ajax?] to add another question field - use that to insert the question in the database as well.

非要怀念 2024-11-26 21:53:39

像这样的事情就可以解决问题:

<cfloop list="#form.fieldNames#" index="field">
<!--- yes, i used len('question') rather than '8' for readability--->
<cfif len(field) gt len('Question') AND left(lCase(field), len('Question')) eq 'question'  and listLen(field,'-') eq 1>
    <cfset question = form[field]>
    <!--- insert question into database  --->
    <cfloop list="#form.fieldNames#" index="answerField">
        <cfif findNoCase(field, answerField) and listLen(answerField) gt 1>
            <cfset answer = form[answerField] >
            <!--- answer for same question, insert into database --->
        </cfif>
    </cfloop>
</cfif>

我们正在做的是循环表单中的字段并查找问题。当我们找到一个时,我们将其插入数据库。然后我们循环查找该问题的答案并将其插入数据库

我们如何查找问题字段:

 <cfif len(field) gt len('Question') 
    AND left(lCase(field), len('Question')) eq 'question'  and 
    listLen(field,'-') eq 1>

如果该字段比字符串“问题”长
剩下的 8 个字符是“问题”
并且没有包含更多字符的破折号

我们如何找到答案:

 <cfif findNoCase(field, answerField) and listLen(answerField) gt 1>

如果问题字段是名称的一部分
并且它有一个破折号,所以多个元素

有意义吗?

Something like this will do the trick :

<cfloop list="#form.fieldNames#" index="field">
<!--- yes, i used len('question') rather than '8' for readability--->
<cfif len(field) gt len('Question') AND left(lCase(field), len('Question')) eq 'question'  and listLen(field,'-') eq 1>
    <cfset question = form[field]>
    <!--- insert question into database  --->
    <cfloop list="#form.fieldNames#" index="answerField">
        <cfif findNoCase(field, answerField) and listLen(answerField) gt 1>
            <cfset answer = form[answerField] >
            <!--- answer for same question, insert into database --->
        </cfif>
    </cfloop>
</cfif>

What we're doing is looping over the fields from the form and finding questions. When we find one, we insert into the database. Then we loop to find the answers for that question and insert them into the database

How we're finding a question field :

 <cfif len(field) gt len('Question') 
    AND left(lCase(field), len('Question')) eq 'question'  and 
    listLen(field,'-') eq 1>

if the field is longer than the string 'question'
and the left 8 chars is 'question'
and there isn't a dash with more chars

How we find an answer :

 <cfif findNoCase(field, answerField) and listLen(answerField) gt 1>

if the question field is part of the name
and it has a dash, so more than one element

Make sense?

美人迟暮 2024-11-26 21:53:39

我之前也研究过类似的问题,只不过表单上有最多 5 层的层次结构数据。在这种情况下,我们使用 Javascript 构建数据结构并将其转换为 JSON 并通过 AJAX 将其发送到服务器。然后,CF 只需使用 JSONDeSerialize() 将其转换为一行代码中的分层结构体。您正在将更多工作推给客户端,但那是已经定义结构的地方,因此在那里做起来会更容易吗?

I worked on a similar problem before, except we had up to 5 levels of heirarchical data on the form. In that case, we used Javascript to build up a structure of the data and turn it into JSON and send it over AJAX to the server. CF then just used JSONDeSerialize() to turn it into a heirarchical struct-of-structs in one line of code. You're pushing more work into the client, but that's the place where the structure is already defined, so it make be easier to do it there?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文