Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
条件逻辑中的Coldfusion函数_Coldfusion - Fatal编程技术网

条件逻辑中的Coldfusion函数

条件逻辑中的Coldfusion函数,coldfusion,Coldfusion,我的cfc中的函数有问题。当我尝试引入条件逻辑来将查询分配给网格时,它们的行为很有趣。基本上,在URL中,我将有?GRIDID=x,它将告诉cfc要运行哪个函数,但当我在if语句中嵌套closingcffunction标记时,它会抛出一个错误。这是代码 <cffunction name="grabInfo" access="remote" output="false" returntype="any"> <cfargument name="page" required="y

我的cfc中的函数有问题。当我尝试引入条件逻辑来将查询分配给网格时,它们的行为很有趣。基本上,在URL中,我将有
?GRIDID=x
,它将告诉cfc要运行哪个函数,但当我在if语句中嵌套closing
cffunction
标记时,它会抛出一个错误。这是代码

<cffunction name="grabInfo" access="remote" output="false" returntype="any">
  <cfargument name="page" required="yes">
  <cfargument name="pageSize" required="yes">
  <cfargument name="gridsortcolumn" required="yes">
  <cfargument name="gridsortdirection" required="yes">
  <cfargument name="filtercolumn" required="no" default="">
  <cfargument name="filter" required="no" default="">
  <cfargument name="gridID" required="yes">
    <cfif arguments.gridsortcolumn eq "">
      <cfset arguments.gridsortcolumn = "PatientsName" />
      <cfset arguments.gridsortdirection = "asc" />
    </cfif>



<cfif ARGUMENTS.gridID EQ "1">
  <cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>
</cfif>

<cfif ARGUMENTS.gridID EQ "2">
<cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>
</cfif>
我需要这样做的原因是,当GridID EQ 2运行时,我还需要运行其他几个函数,因此我需要关闭该函数并打开另一个函数,如下所示

<cfif ARGUMENTS.gridID EQ "2">
<cfquery name="x" datasource="#dsn#">
    <!--- .... --->
  </cfquery>
<cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
</cffunction>

<cffunction name="otherFunction">
    <!--- .... --->
</cffunction>
</cfif>

在组件中添加其他功能

<cffunction name="grabInfo" access="remote" output="false" returntype="any">
  <cfargument name="page" required="yes">
  <cfargument name="pageSize" required="yes">
  <cfargument name="gridsortcolumn" required="yes">
  <cfargument name="gridsortdirection" required="yes">
  <cfargument name="filtercolumn" required="no" default="">
  <cfargument name="filter" required="no" default="">
  <cfargument name="gridID" required="yes">
  <cfif arguments.gridsortcolumn eq "">
    <cfset arguments.gridsortcolumn = "PatientsName" />
    <cfset arguments.gridsortdirection = "asc" />
  </cfif>

  <cfif ARGUMENTS.gridID EQ "1">
    <cfquery name="x" datasource="#dsn#">
      <!--- .... --->
    </cfquery>
    <cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
  </cfif>

  <cfif ARGUMENTS.gridID EQ "2">
    <cfquery name="x" datasource="#dsn#">
      <!--- .... --->
    </cfquery>
    <!--- call your other functions --->
    <cfset otherFunction(arg1, arg2)>
    <cfset anotherFunction(arg1, arg2)>
    <cfreturn QueryConvertForGrid(qGrabInfo, Arguments.page, Arguments.pagesize)>
  </cfif>
</cffunction>

同一组件中的新功能

<cffunction name="otherFunction" access="remote" output="false" returntype="any">
  <cfargument name="arg1">
  <cfargument name="arg2">
  <!--- do things --->
</cffunction>

<cffunction name="anotherFunction" access="remote" output="false" returntype="any">
  <cfargument name="arg1">
  <cfargument name="arg2">
  <!--- do things --->
</cffunction>

您遇到的主要问题是不了解代码是如何编译的。您的代码不会在运行时执行,也就是说,当计算类似于条件的内容时,需要首先编译代码。要进行编译,代码需要在语法上有效。而你的不是

函数是离散的处理单元,需要自包含。从代码的角度来看,您试图做的事情完全没有意义。它还表明对函数如何工作缺乏理解。它们在声明时不执行(即
/
块,它们在被调用时运行)

马特让你走上了正确的道路,但要重申,你没有这样做:

<cffunction name="mainFunction">
    <!--- some stuff --->
    <cfif someCondition>
        <!--- some other stuff --->
        <!--- finish off --->
        </cffunction>
    <cfelse>
        <!--- different stuff --->
        <cffunction name="theOtherFunction">
            <!--- different function --->
        </cffunction>
        </cffunction><!--- this is for the outer function --->
    </cfif>

那…那是不对的

你想要的是:

<cffunction name="mainFunction">
    <!--- some stuff --->
    <cfif someCondition>
        <!--- some other stuff --->
        <!--- different function --->
        <cfset something = theOtherFunction()>
    </cfif>
    <!--- finish off --->
</cffunction>

<cffunction nname="theOtherFuction">
    <!--- different stuff --->
</cffunction>

请注意每个编码结构是如何自包含的

我认为在深入研究之前,阅读CFML文档和一些基本的编程教程(任何语言)都会使您受益匪浅

另外请注意:尽量避免在业务逻辑中使用基于标记的代码:标记确实更适合视图,这是对早期信息不充分的时代的一种倒退,人们甚至可以使用标记定义函数。经验法则是:视图的标记;逻辑的脚本


阅读和理解下面的注释也很重要。即使您的代码在语法上是正确的,并且可以编译,它仍然不会执行您希望它执行的操作,因为函数与代码的其余部分是分开编译的,所以您的条件仍然不起作用。

我需要执行此操作的原因不,您不需要;-)。您混淆了函数声明和用法。组件的功能应该预先定义,而不是在
cfif
语句中有条件地定义。使用
cfif
控制要调用的函数以及调用时间。它们在声明时不执行(即:
/
说得好。为了进一步说明这一点,即使忽略原始代码中的语法错误-虽然CF可能允许您将完整的函数定义放在cfif标记ie
中-您不应该这样做。@Leigh我还没有测试过,但我记得函数声明是提取和编译的与周围的代码分开,这样的条件性无论如何都是毫无意义的。我想我应该重新测试一下。是的,但这就是重点(因为没有更好的词);-)最初的问题不仅仅是代码必须是“语法有效的”CF将允许你在一个条件中嵌套一个完整的函数声明,这可能会导致AsKER对函数如何工作的误解。即使CF可能认为这类代码在语法上是有效的,正如上面所说的,它是没有意义的。函数将被提取和预编译,所以它不是实际的。不管怎样,我都会照他们的想法去做。@Leigh:Gotcha。更新了答案以引起注意。
<cffunction name="mainFunction">
    <!--- some stuff --->
    <cfif someCondition>
        <!--- some other stuff --->
        <!--- different function --->
        <cfset something = theOtherFunction()>
    </cfif>
    <!--- finish off --->
</cffunction>

<cffunction nname="theOtherFuction">
    <!--- different stuff --->
</cffunction>