CF 中的嵌套查询

发布于 2024-09-09 06:48:41 字数 1892 浏览 3 评论 0原文

我正在使用此代码来显示平台列表。如果在进入页面时指定了 platformID,我想在指定平台下创建一个流派列表。

  1. browser.cfm 是通过指定 platformID 为 1 的链接访问的 browser.cfm
  2. 将列出所有可用平台
  3. 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.

  1. browse.cfm was accessed via a link that specified a platformID of 1
  2. browse.cfm will list all available platforms
  3. 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 技术交流群。

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

发布评论

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

评论(2

短叹 2024-09-16 06:48:41

您可以使用,它们可以嵌套。

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.

秋意浓 2024-09-16 06:48:41

更值得思考的是在两个表之间使用内部联接,组合并检索一个查询中的所有内容,然后使用 cfoutput 的 group 属性来显示结果:

<cfset URL.platformID = int(val(URL.platformID))>

<cfquery name="getPlatformsAndGenres" datasource="#REQUEST.datasource#">
SELECT
    p.platformID AS platformID
    ,p.platformName AS pName
    ,g.genreID AS genreID
    ,g.genreName AS gName
FROM
    platforms p
    INNER JOIN genres g
        ON p.platformID = g.platformID
WHERE
    p.platformID = <cfqueryparam cfsqltype="cf_sql_integer" value="#URL.platformID#">
ORDER BY
    pName
    ,genreName
</cfquery>

Once you have everything in one query, you can use <cfoutput query="getPlatformsAndGenres" group="pName">
to lessen your code:

<ul>
    <li>Browse</li>
    <cfoutput query="getPlatformsAndGenres" group="pName">
    <li>
        <a href="browse.cfm?platformID=#URLEncodedFormat(Trim(platformID))#">#pName#</a>
        <ul>
            <cfoutput>
            <li><a href="browse.cfm?genreID=#URLEncodedFormat(Trim(genreID))#">#gName#</a></li>
            </cfoutput>
        </ul>
        </cfif>
    </li>
    </cfoutput>
</ul>

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:

<cfset URL.platformID = int(val(URL.platformID))>

<cfquery name="getPlatformsAndGenres" datasource="#REQUEST.datasource#">
SELECT
    p.platformID AS platformID
    ,p.platformName AS pName
    ,g.genreID AS genreID
    ,g.genreName AS gName
FROM
    platforms p
    INNER JOIN genres g
        ON p.platformID = g.platformID
WHERE
    p.platformID = <cfqueryparam cfsqltype="cf_sql_integer" value="#URL.platformID#">
ORDER BY
    pName
    ,genreName
</cfquery>

Once you have everything in one query, you can use <cfoutput query="getPlatformsAndGenres" group="pName">
to lessen your code:

<ul>
    <li>Browse</li>
    <cfoutput query="getPlatformsAndGenres" group="pName">
    <li>
        <a href="browse.cfm?platformID=#URLEncodedFormat(Trim(platformID))#">#pName#</a>
        <ul>
            <cfoutput>
            <li><a href="browse.cfm?genreID=#URLEncodedFormat(Trim(genreID))#">#gName#</a></li>
            </cfoutput>
        </ul>
        </cfif>
    </li>
    </cfoutput>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文