Need EXPERT help..i'm stuck. Need to loop through an output query and add quantities until variable.budget is met

Here is my ouput query below.  On this page i'm going to have a budget variable.  let's say the budget is $10,000 for example.  My query is already pulling the correct records.  It is giving me a maximum of 5 records.  What i need to do is add all the costs up, then keep on adding another quantity for each record until the budget is met WITHOUT going over.  it's okay if it falls under budget.  

I'm guessing  I take #form.budget# then if  #cost# LT $10,000 (#form.budget#) add 1 to qty, then Budget - cost, then onto next record...if cost LT remaining form.budget, then add 1 to qty of next record and so on.  

I'd be VERY greatful if someone can point me in the right direction here...

my current query which need to add the the quantities to up to and until budget is met
----------------------------------------------------------------------------------------------------

<cfquery name="getUnits" datasource="people" maxrows="5">
select DISTINCT network
from dbo.units, dbo.subregions, dbo.networks, dbo.timeslots
where units.subregion_id = subregions.subregion_id
and units.network_id = networks.network_id
and units.timeslot_id = timeslots.timeslot_id
and networks.network IN (#ListQualify(networks, "'")#)
</cfquery>

<cfset networkRow = 1>

<cfoutput query="getUnits">

  <cfset currentNetwork = network>

<cfquery name="getUnits" datasource="people" maxRows="1">
  SELECT *
  from dbo.units, dbo.subregions, dbo.networks, dbo.timeslots
where units.subregion_id = subregions.subregion_id
and units.network_id = networks.network_id
and units.timeslot_id = timeslots.timeslot_id
and networks.network IN (#ListQualify(networks, "'")#)
AND networks.network =  <cfqueryparam cfsqltype="cf_sql_varchar" value="#currentNetwork#">
and units.subregion_id IN (#form.subregion_id#)
order by #priority#
</cfquery>

 <cfif getUnits.recordcount>
    <cfloop query="getUnits">
      #networkRow# - #currentNetwork# - #subregion_name# - #timeslot# - #cost#  <br>
    </cfloop>

    <cfset networkRow = networkRow + 1>
  </cfif>
</cfoutput>
tags266Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

incapitalCommented:
You could use a conditional loop, where the condition is that your running total is less than form.budget. It would look something like this:

<cfset variables.runningTotal = 0>
<cfset variables.counter = 1>
<cfloop condition="variables.runningTotal LTE form.budget" >
    <cfset variables.runningTotal = variables.runningTotal + qryGetUnits.cost[variables.counter]>
    <cfoutput>#getUnits.networkRow[variables.counter]# - #getUnits.currentNetwork[variables.counter]# - #getUnits.subregion_name[variables.counter]# - #getUnits.timeslot[variables.counter]# - #getUnits.cost[variables.counter]#</cfoutput>
    <cfset variables.counter = variables.counter + 1>
</cfloop>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
incapitalCommented:
Oh, and you might want to add an additional condition to your loop to include your record count to exit the loop if you've run through all your records...

<cfloop condition="variables.runningTotal LTE form.budget AND variables.counter LTE getUnits.recordCount" >
0
tags266Author Commented:
looking good incapital...where would i put the second contition in relation to the first condition?  And where would i fit this all within my current query above???
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

incapitalCommented:
It can all go in the cfloop tag as part of the condition, as below:

<cfset variables.runningTotal = 0>
<cfset variables.counter = 1>
<cfloop condition="variables.runningTotal LTE form.budget AND variables.counter LTE getUnits.recordCount" >
    <cfset variables.runningTotal = variables.runningTotal + qryGetUnits.cost[variables.counter]>
    <cfoutput>#getUnits.networkRow[variables.counter]# - #getUnits.currentNetwork[variables.counter]# - #getUnits.subregion_name[variables.counter]# - #getUnits.timeslot[variables.counter]# - #getUnits.cost[variables.counter]#</cfoutput>
    <cfset variables.counter = variables.counter + 1>
</cfloop>
0
tags266Author Commented:
i guess i'm just confused on where to put it within my current query in my first post.
0
incapitalCommented:
Do you want to break out of your query loop when you max out the budget?
0
tags266Author Commented:
Yes...i want to loop until the budget is met then stop
0
pcaiooCommented:
try this


<cfparam name="form.budget" default="10000">
<cfif getUnits.recordcount>
<cfset total = 0>
<cfloop query="getUnits">
<cfset total = total + cost>
<cfif total gt form.budget>
<cfbreak>
</cfif>
#networkRow# - #currentNetwork# - #subregion_name# - #timeslot# - #cost#  <br>
</cfloop>
<cfset networkRow = networkRow + 1>
</cfif>

this mean if budget after the current row is over it will quit the loop and not add the current row
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.