Coldfusion 将数据循环到多列和多行

Posted

技术标签:

【中文标题】Coldfusion 将数据循环到多列和多行【英文标题】:Coldfusion Looping Data Into Multiple Columns and Rows 【发布时间】:2016-10-27 15:39:34 【问题描述】:

所以这是我的问题,我从数据库中提取到我的网站。我的数据库中需要显示的键/属性(列)是 PostedDate、DataImage 和 Source。我希望它们作为一个 div 输出,顶部是张贴日期,中间是图像,底部是源,我必须这样做。但是,我需要它们循环 3 列并每 3 列创建一个新行,以便将其分开,而不仅仅是一个大列或行。

所以它会像这样 [PostedDate, Image, Source] [PostedDate, Image, Source] 等。多行 3 列

这就是我想要让它看起来的样子:

这是我当前的代码,但是当我刷新网页时,它给了我 404 或 500 错误:

        <!--- set the number of colums you wish to have --->
<cfset cols = 3> 
<cfset totalRows = ceiling(UIData.RecordCount / cols)>
<cfset output = 1>
<table width = "100%" border="0" align="center" cellpadding="2" cellspacing= "2">            
<cfloop from = "1" to = "#totalRows#" index = "thisRow">
<tr>
    <cfloop from = "1" to = "#cols#" index = "thisCol">

    <td width = "<cfoutput>#numberformat((100/cols), 99)#</cfoutput>%" align="center" nowrap style = "border: 1px solid #ccc;">

        <cfif output != UIData.recordCount>
            <cfoutput> 
            <div> <font style="font-size: .8em; line-height: .6em; padding-bottom: .8em;"><strong> #PostedDate# </strong></font> </div>
         <div> <img src="http://www.iowalum.com/uidata/images/#DataImage#"    style="padding-top:10px; padding-bottom:10px;" /> </div>
         <div id ="text"> #Source# </div> </cfoutput>
        <cfelse>
        <!--- use <br> to display an empty cell --->
            <br>
        </cfif>
        <!--- increment counter to the next record in this example if we started on the first cell of the first row it would be 1(a), then 4(d), then 7(g) and so on if this was the firs cell on the second row it would be 2(b), 5(e), 8(h), continue... --->
        <cfset output = output + totalRows>
    </td>
    </cfloop>
    <!--- this little bit tells where to start the next row. if we just finished the first row output would be 2(b) --->
    <cfset output = thisRow + 1>
</tr>
</cfloop>

【问题讨论】:

【参考方案1】:

这种方法可能更简单。

<cfset ItemsPerRow = 3>     
<tr>
<cfoutput query="your query">
<td>#output something#    </td>
<cfif currentrow mod ItemsPerRow is 0>
</tr><tr>
<cfelseif currentrow is recordcount>
</tr>
</cfif>
</cfoutput>
<tr>

【讨论】:

哇,谢谢!我注意到在我的代码中我忘了输入 cf 查询名称,它很接近但略有偏差。

以上是关于Coldfusion 将数据循环到多列和多行的主要内容,如果未能解决你的问题,请参考以下文章

将单行转换为多行

循环遍历具有多列和多行的数组

熊猫数据框和多行值打印为字符串?

Xamarin 表单:显示和绑定具有多行和多列的列表/网格的数据

怎么将excel中两列转换成多行多列

如何将多行和多列绑定到列表视图?