Evaluate within CFSET

Ask a Question related to Coldfusion - Advanced Techniques, Design and Development.

  1. #1

    Default Evaluate within CFSET

    I'm looping through a list of sites. I've written queries to count the number
    of live and pending articles in tables that begin with the site's abbreviation.
    (See
    [url]http://www.macromedia.com/cfusion/webforums/forum/messageview.cfm?catid=7&thread[/url]
    id=1014489&forumid=1 for more details.)
    The code #Evaluate(Abbrev &"Live.NumberOfLiveArticles")#+ gives me 0+ 8+ 0+
    0+ 0+ 15+
    but I can't get it to work within a CFSET tag where I'd like to get the total.

    CODE:


    <cfloop query="BBIPsites">
    <cfoutput> #Evaluate(Abbrev &"Live.NumberOfLiveArticles")#+ </cfoutput>
    </cfloop>
    Where within the loop should I stick this?
    <cfset LiveTotal = #Evaluate(Abbrev &"Live.NumberOfLiveArticles")#+>

    H3ath0r Guest

  2. Similar Questions and Discussions

    1. cfset Help!
      Hello everyone, I am just starting to learn ColdFusion, and I am learning how to use the <cfset> tag. I tried hard, but I can't figure out what's...
    2. cfset
      Hi guys, I was just wondering if I use <cfset session.something=0> and I want to later set session.something to equal something else is it ok to...
    3. cfset question
      I am setting a variable correctly with cfscript and I'm trying to do the equalivant with cfset. Here's how I set it in cfscript: oElement =...
    4. CFSET and Quotation Marks
      I have a page that generates an html attachment and sends it in email to an admin. The page is an asp page. my problem is that the asp page code...
    5. <cfset> user info.
      Hey guys, i am trying to create a website that has dynamic text in it. For example "Welcome (username here)". I have started using the <cfset>'s...
  3. #2

    Default Re: Evaluate within CFSET

    You need to evaluate twice. Once to get the string "0+ 8+ 0+ 0+ 0+ 15" from
    the
    dynamic variable, then again to evaluate the value of the string, The second
    evaluate has no quotes.

    <cfset LiveTotal = Evaluate(Abbrev &"Live.NumberOfLiveArticles")>
    <cfset LiveTotal = Evaluate(LiveTotal)>

    You can't have that trailing "+" in there. I don't know what it's for but
    it will cause errors trying to do math operations.

    OldCFer Guest

  4. #3

    Default Re: Evaluate within CFSET

    You don't need Evalutate(), you should use

    #VARIABLES[Abbrev & "Live.NumberOfLiveArticles"]#

    instead of

    #Evaluate(Abbrev &"Live.NumberOfLiveArticles")#

    as for where to put <cfset LiveTotal = LiveTotal + #VARIABLES[Abbrev &
    "Live.NumberOfLiveArticles"]#>, you should put that anywhere after the query
    that gets the information but still within the loop.

    Try never to use Evaluate() it is slow and can open up security risks, it is
    very rare that with a bit of thought you cannot find an alternative.

    Stressed_Simon Guest

  5. #4

    Default Re: Evaluate within CFSET

    First to answer your question. You could declare a total variable before you
    enter the loop
    <cfset TotalCount = 0>

    and then inside the loop append the count to the running total ..
    <cfset TotalCount = TotalCount + Evaluate(Abbrev &"Live.RecordCount")>

    In looking at your code again, if all you're doing in the loop is outputting
    the totals, you may not actually need to use a dynamic query name at all. You
    could use a single query name and avoid the call to Evaluate() altogether. It
    might also be possible to create a single query to retrieve the counts and
    avoid the separate query on each loop, but ... that depends on what else you're
    doing in the page.






    <!--- Using your original code. Not tested ---->
    <cfloop query="BBIPsites">
    <cfquery name="PendingArticles" datasource="BBN">
    SELECT ID FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 0
    </cfquery>
    <cfquery name="LiveArticles" datasource="BBN">
    SELECT ID FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 1
    </cfquery>
    <cfoutput>
    <strong>#SiteName#</strong><br>
    The number of live articles on #SiteName# is #PendingArticles.RecordCount#<br>
    The number of pending articles on #SiteName# is #LiveArticles.RecordCount#<br>
    <br>
    </cfoutput>
    </cfloop>

    mxstu Guest

  6. #5

    Default Re: Evaluate within CFSET

    mxstu is right you will need to set the LiveTotal equal to zero before your loop!
    Stressed_Simon Guest

  7. #6

    Default Re: Evaluate within CFSET

    Stressed_Simon,

    Is that a feature of MX 7? I tried accessing the query name via the variables
    scope under MX 6.1 and am getting "Element (element name) is undefined in a
    Java object of type class coldfusion.runtime.VariableScope referenced as "
    error.

    mxstu Guest

  8. #7

    Default Re: Evaluate within CFSET

    Really???? I am on MX 6.1 and I use that all the time!

    Try this:-

    <cfset q = QueryNew("test")>
    <cfset QueryAddRow(q)>
    <cfset QuerySetCell(q, "test", "Simon")>

    <cfdump var="#q#">
    <cfdump var="#VARIABLES.q#">
    <cfdump var='#VARIABLES["q"]#'>

    Stressed_Simon Guest

  9. #8

    Default Re: Evaluate within CFSET

    Maybe I just need coffee but ...

    <cfset Abbrev = "ThisNameIsDynamic">
    <cfquery name="#Abbrev#Live" datasource="myDSN" maxrows="1">
    SELECT ID AS NumberOfLiveArticles
    FROM MyTable
    </cfquery>

    <!--- this works -->
    <cfdump var="#Abbrev#Live">

    <!--- this works -->
    <cfoutput>
    <cfdump var="#VARIABLES[Abbrev & 'Live']['NumberOfLiveArticles']#">
    </cfoutput>

    <!--- this doesn't work -->
    <cfoutput>
    #VARIABLES[Abbrev & "Live.NumberOfLiveArticles"]#
    </cfoutput>

    Element ThisNameIsDynamicLive.NumberOfLiveArticles is undefined in a Java
    object of type class coldfusion.runtime.VariableScope referenced as


    mxstu Guest

  10. #9

    Default Re: Evaluate within CFSET

    By combining your answers I got a result that works!

    MXSTU, yesterday I ended up using your suggesting to use COUNT(ID) so the
    queries are a bit different than the ones posted here (see code below).

    Now I'll have to investigate the ramifications of using Evaluate() or
    VARIABLES as Stressed_Simon suggests. I wasn't aware this was a concern. The
    whole application, however, is in a secure area where users are logged in. So
    I'm not too worried about security overall.

    Thanks a tonne for all your help! My code has gotten messier than I like
    because of scope creep. Originally there were only a few sites so I set up the
    database with the articles for each site in a separate table. But now that
    number is growing so I'm finding myself having to loop through all these
    separate tables and wishing all the articles were in ONE table. Ah, growing
    pains.

    CODE:

    <cfset LiveTotal = 0>
    <cfloop query="BBIPsites">
    <cfoutput>
    <cfquery name="#Abbrev#Live" datasource="BBN">
    SELECT COUNT(ID) AS NumberOfLiveArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 1
    </cfquery>
    <cfquery name="#Abbrev#Pending" datasource="BBN">
    SELECT COUNT(ID) AS NumberOfPendingArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 0
    </cfquery>
    <cfset LiveTotal = LiveTotal + Evaluate(Abbrev &"Live.NumberOfLiveArticles")>
    </cfoutput>
    </cfloop>

    H3ath0r Guest

  11. #10

    Default Re: Evaluate within CFSET

    Yeah sorry that should have been:-

    #VARIABLES[Abbrev & "Live"]["NumberOfLiveArticles"]#

    I think I might join you for a coffee!
    Stressed_Simon Guest

  12. #11

    Default Re: Evaluate within CFSET

    H3ath0r,

    Yes, as Stressed_Simon mentioned, evaluate() it is slower among other things,
    so using the variables scope would probably be a better option.

    On the upside, if you do decide to combine the data into one table all you
    have to do is add an INSERT statement to CFLOOP script ;-)

    Stressed_Simon,

    Good thing there is enough coffee left in the pot to go around!

    mxstu Guest

  13. #12

    Default Re: Evaluate within CFSET

    You're both right! See code.
    Now, explain to me why I should go through my code and replace my Evaluate()
    instances with VARIABLE where possible....

    Code:


    <cfset Abbrev = "ThisNameIsDynamic">
    <cfloop query="BBIPsites">
    <cfoutput>
    <cfquery name="#Abbrev#Live" datasource="MyDSN" maxrows="1">
    SELECT COUNT(ID) AS NumberOfLiveArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 1
    </cfquery>

    <!--- this works --->
    <cfdump var="#Abbrev#Live">

    <!--- this works --->
    <cfoutput>
    <cfdump var="#VARIABLES[Abbrev & 'Live']['NumberOfLiveArticles']#">
    </cfoutput>

    <!--- this also works --->
    <cfoutput>
    <cfdump var="#VARIABLES[Abbrev & "Live"]["NumberOfLiveArticles"]#">
    </cfoutput>

    </cfoutput></cfloop>

    H3ath0r Guest

  14. #13

    Default Re: Evaluate within CFSET

    You can't refer to query variables using the variables scope. You may be able to do a
    cfdump using that, but nothing else. Am I missing something?
    OldCFer Guest

  15. #14

    Default Re: Evaluate within CFSET

    This works?

    <cfquery name="test" datasource="#REQUEST.data#">
    SELECT TestimonialText, TestimonialPerson
    FROM Testimonials
    </cfquery>

    <table>
    <cfoutput query="VARIABLES.test">
    <tr>
    <td>#TestimonialText#</td>
    <td>#TestimonialPerson#</td>
    </tr>
    </cfoutput>
    </table>

    Stressed_Simon Guest

  16. #15

    Default Re: Evaluate within CFSET

    It seems like you can. Try it.

    <cfquery name="TEST" datasource="myAccessDSN">
    SELECT ID AS NumberOfLiveArticles
    FROM YourTable
    </cfquery>

    <cfoutput>
    #VARIABLES.TEST.NumberOfLiveArticles#<br>
    </cfoutput>


    mxstu Guest

  17. #16

    Default Re: Evaluate within CFSET

    H3ath0r,

    While we're all questioning the reality of whether or not you can and/or
    should access query variables using the variables scope (myself included ;-) ...

    The primary reason to avoid Evaluate() is efficiency. Evaluate() requires
    some additional processing, so it usually more efficient to retrieve the values
    from an available structure, such as retrieving form field values from the FORM
    structure, rather than using evaluate().

    See
    [url]http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/[/url]
    wwhelp.htm?context=ColdFusion_Documentation&file=0 0000946.htm

    mxstu Guest

  18. #17

    Default Re: Evaluate within CFSET

    Using the variable scope you suggested WORKS, but it means an additional query
    for each site in the loop. This is because my tablename is a dynamic variable.
    Compare the CFSET for LiveTotal versus PendingTotal at the end of this code.
    Both work but I still don't understand why I'd choose one over the other.
    I'll make another pot of coffee for all who've lasted with me this far...
    Code:


    <cfset LiveTotal = 0>
    <cfset PendingTotal = 0>
    <cfloop query="BBIPsites">
    <cfoutput>
    <cfquery name="#Abbrev#Live" datasource="BBN">
    SELECT COUNT(ID) AS NumberOfLiveArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 1
    </cfquery>
    <cfquery name="#Abbrev#Pending" datasource="BBN">
    SELECT COUNT(ID) AS NumberOfPendingArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 0
    </cfquery>

    <cfquery name="TEST" datasource="BBN">
    SELECT COUNT(ID) AS NumberOfLiveArticles
    FROM #TableName#
    WHERE Author = #Session.AuthorID# AND LIVE = 1
    </cfquery>

    <!-- This works -->
    <cfoutput>
    Test output is #VARIABLES.TEST.NumberOfLiveArticles#<br>
    </cfoutput>

    <cfset LiveTotal = LiveTotal + #VARIABLES.TEST.NumberOfLiveArticles#>
    <cfset PendingTotal = PendingTotal + Evaluate(Abbrev
    &"Pending.NumberOfPendingArticles")>

    </cfoutput>
    </cfloop>

    H3ath0r Guest

  19. #18

    Default Re: Evaluate within CFSET

    Okay... so given what I read on
    [url]http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/[/url]
    wwhelp.htm?context=ColdFusion_Documentation&file=0 0000946.htm, then I guess
    since we showed above that I CAN use the variable scope in this case, then I
    SHOULD go ahead and do so.

    Phew. You boys are thorough.
    *smeck* a kiss on the cheek for all!
    Heather

    H3ath0r Guest

  20. #19

    Default Re: Evaluate within CFSET

    CF stores the whole query in the variables scope, so
    #VARIABLES.TEST.NumberOfLiveArticles#<br>
    is the same as
    #TEST.NumberOfLiveArticles#<br>
    so I guess it's implied, but how do you refer to
    #VARIABLES.TEST.NumberOfLiveArticles#<br>
    in bracket notation to eliminate using Evaluate?

    OldCFer Guest

  21. #20

    Default Re: Evaluate within CFSET

    <cfloop query="Variables.TEST">
    #Variables['TEST']['NumberOfLiveArticles'][CurrentRow]#<br>
    </cfloop>
    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139