質問
このコードを使用してプラットフォームのリストを表示しています。ページを入力してplatformIDが指定されている場合は、指定されたプラットフォームの下にあるジャンルのリストを作成します。
- browse.cfmが1 のplatformisを指定したリンクを介してアクセスされました
- browse.cfmはすべての利用可能なプラットフォーム をリストします
-
BROWSE.CFMは、1のPlatformIDの下で利用可能なすべてのジャンルをリストします。
<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>
ただし、この方法を使用することで、無効なネスト設定が取得されています。どうすればいいですか?それとも同じ考えを達成するための別のアプローチはありますか?
ありがとう
マイクエリ:
<!---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>
.解決
<cfloop query="qGetGenres"></cfloop>
を使用することができます。ネストすることができます。
IMO、クエリをループするためのCFOUTPUTを使用することは古いスタイルであり、避けるべきです。CFOutput for Output、LoopingのためのCFLOOP、やはらかに読みやすいコードがあります。
他のヒント
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>
所属していません StackOverflow