Skip to main content
Known Participant
May 30, 2011
Question

CFLOOP and Conditions

  • May 30, 2011
  • 1 reply
  • 5664 views

Hi, all,

I'm sure it's not as difficult as it seems, I think that my main problem is that I can't formulate what I want properly (in Coldfusion language that is).

I have an xml file on a cfc, I turned it into a query. From there I pull two queries

1- Category (listing the different task categories in my files).

2- Task (listing the different tasks according to their category).


Each task take a specific amount of time to perform (Nday).

What I am trying to do, is to take a random amount of tasks in each category, calculate their cumulative time, and display them. I only have 5 days in the week, so the cumulative time can't be over 5 days.

So far my code looks something like this:


<cfinvoke  component="mycfc" method="xmltask" returnvariable = "qrtask">

<cfquery name="cat" dbType="query">
    SELECT Category
    FROM qrtask
    GROUP BY Category
</cfquery>

<cfset tc = 0>
<cfset dd = 0>

    <cfloop query="cat">
        <cfquery name="Task" dbtype="query">
            SELECT Task_Name, Nday
            FROM qrtask
            WHERE category = '#cat.category#'
        </cfquery>
        <cfset displayrow = randrange(1,task.recordcount)>
        <cfoutput query="task" startrow="#displayrow#" maxrows="3">
            #cat.category#

            #Task_Name#

            #Nday#

            <cfset tc = Nday> <cfset dd = dd+tc> #dd#  

        </cfoutput>
     </cfloop>

The whole thing seems to work pretty much the way I want to... except I can't calculate the total per category, I just get the total of the whole thing.

I don't really how to force the cfloop to go from one category to another (so my idea to use something like <cfif dd less than 5> did not really work).

Ideally the query should reloop until "dd" is as close to 5 as possible, then move on to the next category (I can have only one 5 days tasks, five 1 day tasks or anything in between).

I'll exchange my famous chili con carne recipe against any light on the subject... (even though bean and light should not probably mix all that much )

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    May 30, 2011

    Is this perhaps what you wish to get:

    <cfloop query="cat">
    <cfset tc = 0>
    <cfquery name="Task" dbtype="query">
        SELECT Task_Name, Nday
        FROM qrtask
        WHERE category = '#cat.category#'
    </cfquery>
    <cfset displayrow = randrange(1,task.recordcount)>
    <cfoutput query="task" startrow="#displayrow#" maxrows="3">
        #cat.category#
        #Task_Name#
        #Nday#
        <cfset tc = tc + Nday> #tc#  
    </cfoutput>
    </cfloop>

    Kryss2099Author
    Known Participant
    May 30, 2011

    Hi,

    Thanks a lot for your time, it is apreciated.

    It sure is a more interesting solution than I came up with and it does solve a part of the problem, the count does restart for each category.

    The original problem was to look in the query, pull up the tasks according to their Nday as long as the total Nday for each category does not exceed 5. (5 or less as some categoeries just can't reach it).

    I'm not really familiar with cfloop, and don't really grasp the logic behind it, guess I'll have to look it up more closely, I know that cfif does not work in this case (anything I tried just breaks the loop).

    Again Thank you

    BKBK
    Community Expert
    Community Expert
    May 30, 2011

    You could just replace the line <cfset tc = tc + Nday> with

    <cfif tc LT 5>
    <cfset tc = tc + Nday>
    </cfif>

    This will ensure the sum of the Ndays doesn't exceed 5.