如何在ColdFusion中将循环转换为数组以删除重复项?

时间:2016-06-09 11:59:59

标签: coldfusion

我是ColdFusion新手,我尝试使用<cfoutput> - 标签中的group-attribute删除下面脚本输出中的重复项,但不知何故不起作用:

<cfoutput query="show_books" group="titleno">

所以我尝试通过SQL对重复项进行分组:

GROUP BY books.ean

但不幸的是,这既不起作用。

所以我想现在,我需要将下面的循环转换为数组或列表,将第一次搜索的结果写入数组/列表,对其进行排序,然后删除重复项并从中执行最终输出数组列表。但我不知道如何在ColdFusion中做到这一点。所以任何帮助都表示赞赏!

该剧本来自一个展示书籍的网站。当您打开书籍的详细信息页面时,您会在该详细信息页面的底部看到由同一作者编写的其他书籍。问题是,它只适用于一位作者。还有两位或更多位作者,但如果他们还没有写过多本书,那么这只是(而且这就是难题)。所以例如我们有一个由三位作者组成的团队,他们一起写了四本不同的书,我们在输出中看到了12本书,而不是四本......: - (

以下是代码:

<cfloop query="title_data">

    <!-- query database -->
    <cfquery name="show_books" datasource="#database#" dbtype="ODBC">
        select * from books, books_publisher, books_mapping
        where books.ean <> '#ean#' AND
        books_publisher.publisher_id = #title_data.publisher_id# AND
        books_publisher.partner_id = books_mapping.adrno AND
        books.titleno = books_mapping.titleno AND
        (books.visibility = 1 OR books.visibility = 2 OR books.visibility = 20 OR books.visibility = 95 OR books.visibility = 102 OR books.visibility = 107)
        order by books.title asc, books.ean asc
    </cfquery>

    <cfoutput query="show_books"> 

        <!-- load publisher -->
        <cfquery name="show_mapped" dataSource="#database#" dbtype="ODBC"> 
            select * from books_mapping 
            WHERE titleno = #show_books.titleno# AND
            (role = '1' OR role = '2')
            order by books_mapping.ranking
       </cfquery> 
       <cfset authorteam = #show_mapped.recordcount#>

        <!-- show title data of books -->
        <table>
        <tr><td>
               <a href="index.cfm?view=3&ean=#ean#"><img src="./covers/small/#cover#" class="shadow"></a>
            </td>
            <td>
               <cfloop query="show_mapped"> 

                  <cfquery name="show_publisher" dataSource="#database#" dbtype="ODBC"> 
                     select * 
                     from   books_publisher 
                     WHERE  partner_id = #show_mapped.adrno# 
                  </cfquery> 
                  <cfloop query="show_publisher"> 
                     <cfset authorteam = authorteam -1>
                     <a href="index.cfm?view=6&author_id=#publisher_id#" class="authors">#publisher_prename# #publisher_surname#</a> 
                     <cfif authorteam neq '0'>|</cfif>
                  </cfloop> 
             </cfloop> 
             ....
           </td>
        </tr>
        <tr><td align="right">#price_eur# EUR</td>
            <td> ...
                <a href="index.cfm?view=3&ean=#ean#" class="further_button">Details</a>
            </td>
       </tr>
     </table>
   </cfoutput>

</cfloop>

此外,如果有另一种方法或解决方法来删除输出中的重复条目,那对我来说将是非常有帮助的。我打开各种建议只是为了让它发挥作用。

1 个答案:

答案 0 :(得分:0)

如果您不需要主键值,请更改:

select * from books, books_publisher, books_mapping

到此:

select distinct justTheFieldsYouNeed

如果您这样做,请保持查询的方式,并使用cfoutput的group属性来管理您的显示。

<cfoutput query="yourQuery" group="someField">
#groupedData#
<cfoutput>
#ungroupedData#
</cfoutput>
</cfoutput>