有一些麻烦理解循环 [英] Having some trouble understanding loops

查看:186
本文介绍了有一些麻烦理解循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我出于某种原因创建了这个查询,因此没有更新任何查询。

I created this for some reason neither one of the queries are being updated

<cfloop index="i"  from="1" to="#ArrayLen(location)#">

    <cfif location[i] NEQ "" AND #locationID# EQ "" >

        <cfquery Name="UpdateAddActivity" DATASOURCE="#DS#">        
            INSERT INTO tblProjectLocations
            (   
                projectID,
                locationID
            )
            VALUES
            (   
                #ProjectName#,
                #location[i]#
            )
        </cfquery>

    </cfif>

    <cfif location[i] EQ "" AND #locationID# NEQ "" >

        <cfquery Name="UpdateAddActivity" DATASOURCE="#DS#">        
            DELETE FROM tblProjectLocations
            WHERE locationID = #locationID# AND projectID = #ProjectName# 
        </cfquery>              

    </cfif>

</cfloop>

我可以正确循环吗?对我来说,累加器永远不会更新,但循环是这样在我看过的每一个地方。

Am I looping correctly? It doesn't seem like to me that the accumulator is ever going to be updated but loops are done this way every place that I've looked.

推荐答案

您的 cfloop 标记很好 - 您只有需要一个基本循环的索引/从/到属性。

Your cfloop tag is fine - you only need index/from/to attributes for a basic loop.

index 变量递增处理)在结束标签的位置。或者换句话说,对于(包括)之间的每个索引值,执行一次身体代码, 。

The index variable is incremented (and the loop re-processed) at the position of the closing tag. Or to put it another way, the body code is executed once for each index value between from and to (inclusive).

有关信息,您可以通过指定步骤属性来更改默认增量

For information, you can change the default increment (of 1) by specifying the step attribute (though that obviously doesn't make sense for an array loop).

当你的代码没有按预期执行时,你可以使用 dump tag:

When your code isn't performing as expected, you can debug it with the dump tag:

<cfloop ... >
    ... 
    <cfdump var=#locationID# abort />
    ...
</cfloop>

abort 属性将停止处理循环将不会迭代,并且将返回当前页面内容(这是分别指定 cfabort 标记的缩写。

The abort attribute will stop processing - the loop will not iterate and the current page content will be returned (it's shorthand for specifying cfabort tag separately.

您可以使用多个转储和标签属性来帮助识别哪些是,但显然如果使用abort属性确保只有

You can use multiple dumps, and the label attribute to help identify which is which, but obviously if using abort attribute make sure only the last one has it.

如上所述 locationID 未在您提供的代码段中定义,所以可能是问题。

As has been mentioned locationID isn't defined in the snippet you've provided, so may be the issue.

有时空格会导致问题 - 你可能想使用trim函数来确保你处理空字符串(虽然盲目包裹修剪函数到处都是丑陋的 - 总是尽量避免引入空格)。

Sometimes spaces can cause issues - you may want to use the trim function to ensure you're dealing with empty strings (though blindly wrapping trim functions everywhere is ugly - always try if possible avoid introducing spaces).

循环你只有一种类型的cfloop - 有其他

The from/to loop you've got there is only one type of cfloop - there are others.

具体来说,当你不需要数字索引时,有一个简写的数组循环:

Specifically, when you don't need the numeric index, there is a shorthand array loop:

<cfloop index="CurLocation" array=#Location# >
    ...
</cfloop>

这相当于:

<cfloop index="i" from=1 to=#ArrayLen(Location)# >
    <cfset CurLocation = Location[i] />
    ...
</cfloop>

但没有未使用的 i 变量。 (如果你需要 i 变量,坚持从/到。)

But without the unused i variable. (If you need the i variable, stick to from/to.)

注意,几乎总是写 index =local.i index =local.CurLocation范围。这不是循环唯一的 - 它适用于任何创建变量的标签。你也可以在循环之前做< cfset var i = 0 /> 来做同样的事情。

Note that inside a function you should almost always write index="local.i" and index="local.CurLocation" to ensure the variables are appropriately scoped. This isn't unique to loops - it applies to any tags that create variables. You can also do <cfset var i = 0 /> prior to the loop to do the same thing.

您的代码还有其他一些问题。

There are a couple of other issues with your code.

最重要的是,您所显示的代码可能存在SQL注入的风险。您几乎不应该使用裸散列写入SQL,而是使用 cfqueryparam 标记来解决此问题,而是参数化查询。 (在您不能使用参数(例如 ORDER BY )的情况下,请确保您已经对任何动态文本进行了适当的清理。

Most importantly, the code you're showing is potentially at risk of SQL injection. You should almost never write SQL with bare hashes in, and instead parameterise your queries - using cfqueryparam tag - to solve this. (In the situations where you can't use parameters (e.g. within ORDER BY) make sure you have appropriately sanitized any dynamic text.

不太重要 - 它不会改变代码的工作原理,但是出现缺乏经验和理解 - 是 locationID 。一个简单的解释是,你通常只需要在字符串中 s(即内容将被视为文本,而不是一个变量值。)

Less important -- it doesn't change how the code works, but does betray a lack of experience and understanding -- are the superfluous hashes around locationID. A simplified explanation is that you generally only need #s inside strings (i.e. where the contents would otherwise be treated as text, rather than being a variable's value.)

这篇关于有一些麻烦理解循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆