ColdFusion:您可以使用recordCount从查询中提取唯一记录吗?

时间:2012-12-12 12:15:34

标签: coldfusion

这是一个棘手的问题,然而,我的大多数评级乐队的页面按照他们被评级的高度显示乐队标识。我唯一的问题是我想通过使用1到10的cfloop来计算记录,因为它被分成两列,一个计数从1到9,另一个从2到10,每个都有步骤2。

有人可以帮我这个吗?如果我只是提到它而感到困惑,那就试着澄清我的意思。

    <DIV class="community_middle">

    <cfoutput query="top10MostRated">
        <cfloop from="2" to="10" index="i" step="2">
        <DIV class="communityContent">
                            #chr(i)#
            <IMG src="logo/#top10MostRated.Logo#" alt="#top10MostRated.Name#" width="100%" height="100%"></IMG>
        </DIV>
        <BR/>
        </cfloop>
    </cfoutput>

</DIV>

5 个答案:

答案 0 :(得分:2)

如果你想单独编写奇数/偶数列表,那么你可以使用查询的currentrow属性和模运算符(%)来计算行是奇数还是偶数:

<cfloop query="topBands>
  <cfif topBands.currentRow % 2 = 1>
    <!--- do your odd number output here --->
  </cfif>
</cfloop>
<cfloop query="topBands>
  <cfif topBands.currentRow % 2 = 0>
    <!--- do your even number output here --->
  </cfif>
</cfloop>

答案 1 :(得分:2)

我认为这些答案可以解决您问题的并排部分,但不解释“同一图像”问题。他们的代码写得正确,但没有解释原因。

您的代码:

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


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

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


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

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

答案 2 :(得分:0)

Ben Nadel正好有一个帖子。链接here 对此的细分是

<cfloop query="top10MostRated">
    <cfif top10MostRated.CurrentRow MOD 2>
        <!--- Add to the "odd list" --->
    <cfelse>
        <!--- Add the record to the "even list" --->
    </cfif>
</cfloop>

然后你将有2个列表 oddList evenList 。然后只是显示它们。

答案 3 :(得分:0)

我会采用不同的方式。目标是并排记录1和2,我在@ barnyr的回答中没有看到。

<cfoutput>
<cfloop from="2" to="topbands.recordcount + 1" index = "i" step="2">
    #topbands.fieldname[i-1]#  
    <cfif i lte topbands.recordcount>
        #topbands.fieldname[i]# <br />
    </cfif>
</cfloop>
</cfoutput>

答案 4 :(得分:0)

听起来你想得到的是偶数记录的集合和奇数记录的集合。在Coldfusion 10或Railo 4中,您可以使用Underscore.cfc中的groupBy()将查询结果拆分为可管理的子集,如下所示:

_ = new Underscore();// instantiate the library
groupedBands = _.groupBy(topBands, function (val, index) {
   return index % 2 ? "odd" : "even";
}); 

这将返回一个包含两个元素oddeven的结构,每个元素包含一个奇数或偶数的记录数组。示例结果:

{
   odd: [{name: "Band one"}, {name: "Band three"}],
   even: [{name: "Band two"}, {name: "Band four"}]
}

将结果拆分为逻辑子集使代码更具可读性:

<cfoutput>
   <cfloop from="1" to="5" index="i">
      <div class="left">#groupedBands.odd[i].name#</div>
      <div class="right">#groupedBands.even[i].name#</div>
   </cfloop>
</cfoutput>

如果需要,您还可以在页面上的其他位置使用这些子集。

注意:我写了Underscore.cfc

相关问题