且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ColdFusion:您能否使用 recordCount 从查询中提取唯一记录?

更新时间:2023-01-30 21:34:05

我认为这些答案解决了您问题的并排部分,但没有解释相同图像"问题.他们的代码写得对,但没有解释原因.

I think these answers address your side-by-side part of your question but does not explain the "same image" issue. Their code is written correctly but does not explain the reason.

您的代码:

        <IMG src="logo/#top10MostRated.Logo#" 
             alt="#top10MostRated.Name#" 
             width="100%" height="100%"></IMG>


... 如果您只在 <cfloop query = "top10MostRated"><cfoutput query = "top10MostRated"> 块内,那就没问题了.原因是因为在这些类型的块中,CF 足够聪明,可以知道您想要当前行的数据.这将与以下内容相同:


... would be fine if you were only inside a <cfloop query = "top10MostRated"> or <cfoutput query = "top10MostRated"> block. The reason is because inside these types of blocks CF is smart enough to know you want the data for the current row. It would be the same as:

        <IMG src="logo/#top10MostRated.Logo[top10MostRated.currentRow]#" 
             alt="#top10MostRated.Name[top10MostRated.currentRow]#" 
             width="100%" height="100%" />


因为您将 to/from cfloop 嵌套在 <cfoutput query = ""> 块中,所以您会得到意想不到的结果.您现有的代码总是要求您的外部循环提供的记录.因此,您会看到相同的图像 5 次.(使用提供的任何优秀示例将帮助您摆脱这种情况)但是,您可以从 cfoutput 中删除查询,并简单地要求 CF 向您显示循环中正确行的值,使用您的索引(您将索引设置为i"),因此下面会显示与您的循环相对应的图像.


Because you're nesting the to/from cfloop inside a <cfoutput query = ""> block, you are getting unexpected results. Your existing code is always asking for the record provided by your outer loop. Hence you see the same image 5 times. (using any of the fine examples provided will help you get out of this) but, you can remove the query from your cfoutput and simply ask CF to show you the value for the correct row in your loop using your index (you set your index to "i") so the below would show you the image that corresponds to your loop.

        <IMG src="logo/#top10MostRated.Logo[i]#" 
              alt="#top10MostRated.Name[i]#" 
              width="100%" height="100%" />