CFOUTPUT 循环

huangapple go评论50阅读模式
英文:

CFOUTPUT looping

问题

我试图显示和处理来自数据库的26行记录,并按以下方式处理这些行:

<cfquery datasource="mydata" username="myuser" password="mypass" name="mylist" cachedWithin="#createTimeSpan( 0, 0, 5, 0 )#">
    SELECT field1,field2
    WHERE product = "myproduct"
    ORDER BY idfield DESC LIMIT 26
</cfquery>

<!--- 处理它们 --->
<cfoutput query="mylist" maxRows=10>
	<!--- 获取最新的6条记录 --->
	<cfif mylist.CurrentRow lte 6>
        <!--- 对最新记录执行特殊操作 --->
		<cfif mylist.CurrentRow eq 1> 
            做一些特殊的事情
			<cfoutput>
				mylist.CurrentRow - #mylist.CurrentRow#<br> 
			</cfoutput>
		<cfelse> 
            做其他事情
		</cfif>
		<cfif mylist.CurrentRow lte 3>
            做一些事情   
		</cfif>
	</cfif>
	做一些事情
</cfoutput>

我希望你能找到解决问题的线索。

英文:

I am trying to display and process 26 rows of records from the database and process the rows like this:

<cfquery datasource="mydata" username="myuser" password="mypass" name="mylist" cachedWithin="#createTimeSpan( 0, 0, 5, 0 )#">
    SELECT field1,field2
    WHERE product = "myproduct"
    ORDER BY idfield DESC LIMIT 26
</cfquery>

<!--- process them --->
<cfoutput query="mylist" maxRows=10>
	<!--- get the latest 6 record --->
	<cfif mylist.CurrentRow lte 6>
        <!--- do something special with the latest record --->
		<cfif mylist.CurrentRow eq 1> 
            do something special
			<cfoutput>
				mylist.CurrentRow - #mylist.CurrentRow#<br> 
			</cfoutput>
		<cfelse> 
            do something else
		</cfif>
		<cfif mylist.CurrentRow lte 3>
            do something   
		</cfif>
	</cfif>
	do something
</cfoutput>

I was expecting to see one line of output like this:

mylist.CurrentRow - 1 

But instead I got this:

mylist.CurrentRow - 1
mylist.CurrentRow - 2
mylist.CurrentRow - 3
mylist.CurrentRow - 4
mylist.CurrentRow - 5
mylist.CurrentRow - 6
mylist.CurrentRow - 7
mylist.CurrentRow - 8
mylist.CurrentRow - 9
mylist.CurrentRow - 10
mylist.CurrentRow - 11
mylist.CurrentRow - 12
mylist.CurrentRow - 13
mylist.CurrentRow - 14
mylist.CurrentRow - 15
mylist.CurrentRow - 16
mylist.CurrentRow - 17
mylist.CurrentRow - 18
mylist.CurrentRow - 19
mylist.CurrentRow - 20
mylist.CurrentRow - 21
mylist.CurrentRow - 22
mylist.CurrentRow - 23
mylist.CurrentRow - 24
mylist.CurrentRow - 25
mylist.CurrentRow - 26

This is really puzzling. Because it is supposed to display only if the row is equal to 1. Why am I seeing all these rows?

Secondly, why am I seeing 26 rows, when I set the maxrows to 10?

Hope someone can enlighten me on this. Thanks in advance.

答案1

得分: 1

首先,maxrows 限制查询变量中的结果数量,该变量在从数据库返回后生效。

如果您只返回单个结果,那么这对查询的性能几乎没有影响。

ColdFusion 允许您使用 CFQUERYPARAM 标签传递绑定参数。

例如:

<cfquery name="q">
    选择 property1,property2,property3 
    从 yourTable 
    WHERE RowID = <cfqueryparam value="#NumericVariable#" cfsqltype="CF_SQL_INTEGER" />
</cfquery>

通过提供绑定参数并指定要返回的属性,您可以希望提高数据库的速度。这可能允许更好的查询缓存和性能改进,具体取决于您使用的数据库引擎。

我会添加一般来说使用 CFQUERYPARAM 比不加限定的变量更安全,避免潜在的 SQL 注入攻击。

其次,您获取所有结果的原因是内部 cfoutput。

<cfoutput>
 mylist.CurrentRow - #mylist.CurrentRow#<br> 
</cfoutput>

我猜更好的解决方案是将您的查询设为这样:

<cfquery datasource="ECS360"  name ="mylist">
  选择 *
  从 employee.employee
  ORDER BY employeenumber DESC
</cfquery>

<cfoutput query="mylist" maxRows="26">
<br/>
<!--- 获取最新的 6 条记录 --->
    <cfif #mylist.CurrentRow# 小于等于 6>
        <!--- 对最新记录执行特定操作 --->
        当前行 #mylist.currentrow#
        <cfif #mylist.CurrentRow# eq 1> 
            执行特殊操作
                #mylist.CurrentRow# - #mylist.CurrentRow#<br> 
        <cfelse> 
        当前行 #mylist.currentrow#
            执行其他操作
        </cfif>
        <cfif #mylist.CurrentRow# 小于等于 3>
            执行一些 123  
        </cfif>
    </cfif>
</cfoutput>
英文:

First of all, the maxrows limits the number of results in the query variable after its returned from the database.

If you are only ever returning a single result then this is not going to have any impact at all on the performance of your query.

ColdFusion does allow you to pass bind parameters using the CFQUERYPARAM tag.

For example:

<cfquery name="q">
    SELECT property1, property2, property3 
    FROM yourTable 
    WHERE RowID = <cfqueryparam value="#NumericVariable#" cfsqltype="CF_SQL_INTEGER" />
</cfquery>

You can hope to improve the speed on your database by providing a bind parameter and specifying the properties to return. This may allow for better query caching and performance improvements depending on the database engine you are using.

I'd add its generally more secure to use CFQUERYPARAM than to leave variables unqualified and potentially open to SQL injection attacks.

Second of all, the reason why you're getting all the results is because the inner cfoutput

<cfoutput>
 mylist.CurrentRow - #mylist.CurrentRow#<br> 
</cfoutput>

What i guess would be a better solution is to have your query like this:

<cfquery datasource="ECS360"  name ="mylist">
  select *
  from employee.employee
  ORDER BY employeenumber DESC
</cfquery>

<cfoutput query="mylist" maxRows="26">
<br/>
<!--- get the latest 6 record --->
    <cfif #mylist.CurrentRow# lte 6>
        <!--- do something special with the latest record --->
        currnet row #mylist.currentrow#
        <cfif #mylist.CurrentRow# eq 1> 
            do something special
                #mylist.CurrentRow# - #mylist.CurrentRow#<br> 
        <cfelse> 
        currnet row #mylist.currentrow#
            do something else
        </cfif>
        <cfif #mylist.CurrentRow# lte 3>
            do something 123  
        </cfif>
    </cfif>
</cfoutput>

huangapple
  • 本文由 发表于 2023年5月7日 04:35:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191020.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定