Skip to main content
October 22, 2008
Answered

Is there a way to test for the first calling of a function?

  • October 22, 2008
  • 3 replies
  • 398 views
Hi,

I created a function (using other functions I created) to build a dynamic hierarchy tree menu based on parentID and itemID etc etc - I'm sure you're familiar with this concept. It works great, and there's no problem there.

However, I do this all by building it into a string, then dumping it on the page. Yet again, this all works fine. However, because the function gets called recursively, my class, class="tree" gets repeat accross every occurance of the opening UL tag.

I only want this class to be applied to the first UL, but because this is recursive, I'm not sure of a way to detect if the function is being called for the first time or 5th time, etc. I want to say something like:

<cfif myFunction is being called the first time>
add the class
<cfelse>
don't add the class
</cfif>

Please see my code attached. It is fully working. Please don't worry about what all the other functions do etc, they are working fine, but I need a way or variable to test against.

Any ideas?

Many thanks,
Michael.
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    Kapitaine wrote:
    > Hmmm, I bet the solution is simple but my mind is frazzled! Haha.
    >

    Yes it is simple, you test the boolean and it will only be true if it is
    set to true for that one recursion.

    <cffunction name="somethingStupid">
    <cfargument name="aVar" type="numeric" required="yes">
    <cfargument name="addClass" type="boolean" required="no" default="false">

    <cfset var theReturn = "">

    <cfif aVar GT 0>
    <cfset theReturn = somethingStupid(arguments.aVar - 1)>
    </cfif>

    <cfreturn theReturn & ":" & addClass & "-" & aVar>
    </cffunction>

    <cfoutput>#somethingStupid(5,true)#</cfoutput>

    3 replies

    Inspiring
    October 22, 2008
    > a dynamic
    > hierarchy tree menu based on parentID and itemID etc etc

    Is it too late to revise the way you are implementing your hierarchy? This
    approach is very resource intensive (because you need all that recursion),
    and very slow for most operations.

    When dealing with hierarchies, I use the nested set model
    ( http://en.wikipedia.org/wiki/Nested_set_model). Can I recommend you have
    a look, if you have the chance?

    The code is more complicated, but it is really very quick for fetches.
    It's slightly slower for create/update/delete operations, but for web
    sites, the vast majority of operations are reads.

    --
    Adam
    Inspiring
    October 22, 2008
    If the function is building a string, maybe you can if/else something on that string.
    Inspiring
    October 22, 2008
    What about adding a boolean parameter to the function? Something like
    addClass? Then you can set addClass to true or false as relevant.

    I would probably even code this as an optional parameter with a default
    value of false. Then you only need to actually provide it with a value
    of true the first time function is called.

    October 22, 2008
    Hi, thanks for the info. Yeah, I thought about doing this, but I need a variable to test against...i.e, I only want it for the first occurance. I'm not sure how to test for the first occurance of this. Because the function is recursive, it goes over and over adding the class and thus each boolean value I'd set would be true - giving the undesired result.

    Hmmm, I bet the solution is simple but my mind is frazzled! Haha.

    Thanks for the help. Any further suggestions or solutions would be awesome.

    Mikey.