CF 中的嵌套查询
我正在使用此代码来显示平台列表。如果在进入页面时指定了 platformID,我想在指定平台下创建一个流派列表。
- browser.cfm 是通过指定 platformID 为 1 的链接访问的 browser.cfm
- 将列出所有可用平台
browse.cfm 现在将列出 platformID 为 1 下的所有可用类型。
<前><代码>
但是,通过使用此方法,我得到了无效的嵌套配置。我该如何解决这个问题?或者是否有另一种方法可以实现相同的想法?
谢谢
我的询问:
<!---Get platforms--->
<cffunction
name="fGetPlatforms"
access="public"
returntype="query"
output="false"
hint="I get all the platforms">
<!---Local var--->
<cfset qGetPlatforms = "">
<!---Database query--->
<cfquery name="qGetPlatforms" datasource="#REQUEST.datasource#">
SELECT
platforms.platformID,
platforms.platformName AS pName
FROM
platforms
</cfquery>
<cfreturn qGetPlatforms>
</cffunction>
<!---Get genres--->
<cffunction
name="fGetGenres"
access="public"
returntype="query"
output="false"
hint="I get all the genres">
<!---Local var--->
<cfset qGetGenres = "">
<!---Database query--->
<cfquery name="qGetGenres" datasource="#REQUEST.datasource#">
SELECT
genres.genreID,
genres.genreName AS gName
FROM
genres
</cfquery>
<cfreturn qGetGenres>
</cffunction>
I'm using this code to display a list of platforms. If a platformID was specified upon entering the page, I would like to create a list of genres underneath the specified platform.
- browse.cfm was accessed via a link that specified a platformID of 1
- browse.cfm will list all available platforms
browse.cfm will now list all available genres under platformID of 1.
<ul> <li>Browse</li> <cfoutput query="qGetPlatforms"> <li> <a href="browse.cfm?platformID=#URLEncodedFormat(Trim(qGetPlatforms.platformID))#">#qGetPlatforms.pName#</a> <cfif URL.platformID EQ qGetPlatforms.platformID> <ul> <cfoutput query="qGetGenres"> <li><a href="browse.cfm?genreID=#URLEncodedFormat(Trim(qGetGenres.genreID))#">#qGetGenres.gName#</a></li> </cfoutput> </ul> </cfif> </li> </cfoutput> </ul>
By using this approach, however, I'm getting an invalid nesting configuration. How do I fix this? Or is there another approach to achieve the same idea?
Thanks
MY queries:
<!---Get platforms--->
<cffunction
name="fGetPlatforms"
access="public"
returntype="query"
output="false"
hint="I get all the platforms">
<!---Local var--->
<cfset qGetPlatforms = "">
<!---Database query--->
<cfquery name="qGetPlatforms" datasource="#REQUEST.datasource#">
SELECT
platforms.platformID,
platforms.platformName AS pName
FROM
platforms
</cfquery>
<cfreturn qGetPlatforms>
</cffunction>
<!---Get genres--->
<cffunction
name="fGetGenres"
access="public"
returntype="query"
output="false"
hint="I get all the genres">
<!---Local var--->
<cfset qGetGenres = "">
<!---Database query--->
<cfquery name="qGetGenres" datasource="#REQUEST.datasource#">
SELECT
genres.genreID,
genres.genreName AS gName
FROM
genres
</cfquery>
<cfreturn qGetGenres>
</cffunction>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
,它们可以嵌套。IMO,使用 cfoutput 循环查询是旧式的,应该避免。使用 cfoutput 进行输出,使用 cfloop 进行循环,您将获得更具可读性的代码。
You can use
<cfloop query="qGetGenres"></cfloop>
, they can be nested.IMO, using cfoutput for looping over the queries is old style and should be avoided. Use cfoutput for output, cfloop for looping and you'll have more readable code.
更值得思考的是在两个表之间使用内部联接,组合并检索一个查询中的所有内容,然后使用 cfoutput 的 group 属性来显示结果:
more food for thought is to use an inner join between the two tables, combine and retrieve everything in one query and then use cfoutput's group attribute to display the results: