我对这种Coldfusion语言还很陌生,我做了一些教程,我只是按照课程告诉我的去做,但是由于这段代码出现错误,有人可以向我解释为什么?
<cfset carrinho=ArrayNew(1)>
<cfset carrinho[1]=StructNew()>
<cfset carrinho[1].nome="Camisa da Seleção Brasileira">
<cfset carrinho[1].valor=189>
<cfset carrinho[1].quantidade=1>
<cfset carrinho[2]=StructNew()>
<cfset carrinho[2].nome="Chuteira CR7">
<cfset carrinho[2].valor=470>
<cfset carrinho[2].quantidade=2>
<cfset carrinho[3]=StructNew()>
<cfset carrinho[3].nome="Short do Messi">
<cfset carrinho[3].valor=180>
<cfset carrinho[3].quantidade=1>
<cfdump var="#carrinho#">
<cfloop list="#carrinho#" item="name" index="i">
<cfoutput>
#i#:#name#
</cfoutput>
</cfloop>
答案 0 :(得分:4)
原因是<tr><td>
<%= if(rs.getString("delivery0delivery_address_2") != null){
rs.getString("delivery0delivery_address_2")
}
else{
%>
<br>
<%=
}
%>
是一个数组,而不是列表。您可以用许多不同的方式循环它。
使用carrinho
属性进行循环的方法
array
通过<cfset counter=1>
<cfloop array="#carrinho#" index="item">
<cfoutput>#counter#. #item.nome#</cfoutput>
<cfset counter++>
</cfloop>
使用索引循环
ArrayLen
使用<cfloop from="1" to="#ArrayLen(carrinho)#" step="1" index="i">
<cfoutput>#i#. #carrinho[i].nome#</cfoutput>
</cfloop>
cfscript
循环
for
这些只是一些方法。还有许多其他方法可以做到这一点。
答案 1 :(得分:4)
以RRK的答案和Ageax的评论为基础: 在ColdFusion脚本中构建带有嵌套结构的数组非常容易。
注意:
“ //” =评论
“ []” =隐式数组
“ {}” =隐式结构
<cfscript>
// Build the array with structs.
carrinho = [
{
nome : "Camisa da Seleção Brasileira" ,
valor : 189 ,
quantidade : 1
} ,
{
nome : "Chuteira CR7" ,
valor : 470 ,
quantidade : 2
} ,
{
nome : "hort do Messi" ,
valor : 180 ,
quantidade : 1
}
] ;
// Using the counter method to determine the number
counter=1 ;
for(item in carrinho) {
writeoutput(counter & ". " & item.nome & "<br>") ;
counter++;
}
// Using each() member function
carrinho.each(function(element, index) {
writeOutput(index & ". " & element.nome & "<br>");
});
</cfscript>
输出:
- Camisa daSeleçãoBrasileira
- Chuteira CR7
- 梅西霍特
答案 2 :(得分:1)
这是您的解决方案
<cfset carrinho=ArrayNew(1)>
<cfset carrinho[1]=StructNew()>
<cfset carrinho[1].nome="Camisa da Seleção Brasileira">
<cfset carrinho[1].valor=189>
<cfset carrinho[1].quantidade=1>
<cfset carrinho[2]=StructNew()>
<cfset carrinho[2].nome="Chuteira CR7">
<cfset carrinho[2].valor=470>
<cfset carrinho[2].quantidade=2>
<cfset carrinho[3]=StructNew()>
<cfset carrinho[3].nome="Short do Messi">
<cfset carrinho[3].valor=180>
<cfset carrinho[3].quantidade=1>
<cfdump var="#carrinho#">
<cfloop array="#carrinho#" index="i">
<cfoutput>
#i.nome#
</cfoutput>
</cfloop>
您应该使用数组而不是列表。
答案 3 :(得分:-1)
这是一个CF10示例,它循环出数组,然后使用一个内循环来循环出代表数组每个索引的结构:
public CtrlScroll() {
InitializeComponent();
scbMain.Scroll += ( sender, e ) => {
//Normally the if statement whouldn't be needed but the metro srollbar
//has a weird behaviour when the scroll value becomes max
if( scbMain.Value > panel1.Height - this.Height ) {
panel1.Top = -( panel1.Height - this.Height );
}
else {
panel1.Top = -scbMain.Value;
};
};
int maxVertical = panel1.Height;
// SmallChange is typically 1%.
int smallChangeVertical = Math.Max( (int)( maxVertical / 100 ), 1 );
// LargeChange is one page.
int largeChangeVertical = this.Height;
scbMain.Minimum = 0;
scbMain.Maximum = maxVertical;
scbMain.SmallChange = smallChangeVertical;
scbMain.LargeChange = largeChangeVertical;
}