Dynamic conditional cfloop - coldfusion

Maybe I'm just having a bit of Monday morning brainfreeze here but I have the following code...
<cfquery name="getLogins" datasource="myDB">
SELECT * FROM Logins
</cfquery>
<cfloop query="getLogins">
<cfset SESSION["#Trim(getLogins.accountName)#LoggedIn"] = 0 />
<cfloop condition="WHILE SESSION['#Trim(getLogins.accountName)#LoggedIn'] EQUALS 0">
<cfset getLogins.logInAccount = Application.cfcs.Login.logInAccount(getLogins.accountName,getLogins.email,getLogins.password) />
</cfloop>
</cfloop>
I'm getting an error with my condition to do with the Session variable that I'm referencing but I cannot see what I am doing wrong?
Could someone please point me in the right direction.
Many thanks

Dunno why I had a WHILE in my condition - doh! Problem solved

Related

Coldfusion limit cfloop to 10

I'm working with quite a large array of items in a cfloop. I would like to pare this down and add pagination. Is there anyway in my cfloop to limit the array to the first 10?
I have
<cfloop array="#qryItems#" index="index">
I have tried turning it into a condition loop with no luck and a few other things. I haven't touched coldfusion in a while and am a little rusty. Google is not helping haha
I have tried
<cfloop from="1" to="10" array="#qryItems#" index="index">
and have also tried max_rows
<cfloop maxrows="10" array="#qryItems#" index="index">
each time I get the error message
"Attribute validation error for tag CFLOOP."
<cfloop from="1" to="10" index="index">
<!--- Then do your business with array elements qryItems[index], that is, with qryItems[1], qryItems[2],..., qryItems[10] --->
</cfloop>
There is no combination of attributes for cfloop to accomplish what your expecting. As BKBK suggested, you'll need to use a from/to loop to output a select group of records. If I understand your requirements correctly, I would update your cfloop with a new index variable as well, and then set the old variable by referencing the array element.
The two cfloops below output the same data, with the second displaying only the records in the pagination range.
<cfset qryItems = [1,2,3,4,5,6,7,8,9,10,'a','b','c','d'] />
<cfoutput>
<!--- Current loop: Outputs all records --->
<cfloop array="#qryItems#" index="index">
#index#
</cfloop>
<cfset paginationStart = 1 />
<cfset paginationEnd = 10 />
<!--- Only the range of of records requested --->
<cfloop from="#paginationStart#" to="#paginationEnd#" index="indexNumber">
<cfset index = qryItems[indexNumber] />
<!--- code remain the same --->
#index#
</cfloop>
</cfoutput>

Is it possible to have dynamically generated query names in ColdFusion?

What I am trying to do is
<cfloop array="#LOCAL.someArray" index="LOCAL.aString">
<cfset LOCAL.queryName = "uniqueQueryName_" & LOCAL.aString />
<cfquery name="#LOCAL.queryName#" datasource="db" cachedwithin="#CreateTimeSpan(1,0,0,0)#">
SELECT count(*) AS c FROM someTable
</cfquery>
<cfdump var="#LOCAL.queryName#" />
</cfloop>
is this possible, or is there a better way to do it?
Edit
This works with <cfloop query="LOCAL.queryName"> but not when I try to do <cfset ArrayAppend(LOCAL.returnArray, LOCAL.queryName.c) />
There is no need to use evaluate() to do this, and one shouldn't (so I've down-voted that answer, sorry).
All you need to do is use associative array notation:
<cfdump var="#local[qname]#">
If one wants to access a column of that query, it's:
#local[qname][columnName]#
And for a specific cell:
#local[qname][columnName][rowNumber]#
There are very very very few situations in which evaluate() is the correct answer to anything. One cannot rely on the Adobe docs because - unfortunately - an awful lot of it was not written by very experienced ColdFusion developers.
You can dump the query, and I imagine also access it by doing something like this:
<cfloop list="q1,q2,q3" index="qname">
<cfquery name="#qname#" datasource="dsn">
SELECT * from some_table;
</cfquery>
<cfdump var="#Evaluate('#qname#')#" />
</cfloop>
The Evaluate function is what allows you to do what you want.

How to Break out of Cfoutput

I am looping through the results of a query, and I need to limit the number of rows displayed. I need to use cfoutput because I'm using the group attribute, and I cannot use the maxrows because not all rows will be displayed.
I tried to use <cfbreak> inside the <cfoutput>, but that throws an error.
How can I break out of the <cfoutput> loop?
If your group by is only there to remove duplicates from your results I would suggest using your query to cut them down then you can cfloop (select distinct and reducing your returned column list).
If you are using your group by to "group" your results You could run a counter within your loop and a cfif statement inside your first loop to omit later results.
You could fake the group by option in your cfloop by matching value from previous row if you need cfbreak
<cfloop query="queryname">
<cfif queryname.column[currentrow-1] neq queryname.column[currentrow]>
#queryname.column#
</cfif>
</cfloop>
Random note: you can maxrows on any/all levels of your grouped cfoutput
<cfset tmp = querynew('id,dd')>
<cfloop from="1" to="20" index="i">
<cfset queryaddrow(tmp,1)>
<cfset querysetcell(tmp,'id',rand(),i)>
<cfset querysetcell(tmp,'dd',(i mod 4),i)>
</cfloop>
<cfquery dbtype="query" name="tmp">select * from tmp order by dd</cfquery>
<cfoutput query="tmp" group="dd" maxrows="2">#dd#<br
<ul>
<cfoutput maxrows="2" group="id"><li>#id#</li></cfoutput>
</ul>
</cfoutput>
You could use the cfthrow tag to trigger an exception that will allow you to break out of the loop using cfcatch you can then ignore the exception and continue processing. That will give you what you want.
<cftry>
<cfset i = 0>
<cfoutput query="qMyQuery" group="someGroup">
<cfset i = i + 1>
Parent
<cfoutput>
Child
</cfoutput>
<cfif i GTE 10>
<cfthrow type="break">
</cfif>
</cfoutput>
<cfcatch type="break">
<!--- DO NOTHING - THIS IS A HACK FOR NOT BEING ABLE TO USE CFBREAK inside cfoutput. --->
</cfcatch>
</cftry>

insert anchor if row is current date

We have a schedule that is dynamically generated using, in part, this code:
<cfloop from="0" to="6" index="x">
<cfset thisDate = dateFormat(dateAdd("d",x,theDate),"yyyy-mm-dd")>
<cfoutput><tr><td colspan="4" class="date"><strong>#dateFormat(thisDate,"DDDD, M/D")#</strong></td></tr></cfoutput>
<cfif structKeyExists(dayData,thisDate)>
<cfif arrayLen(dayData[thisDate]) gt 0>
<cfloop from="1" to="#arrayLen(dayData[thisDate])#" index="y">
<cfoutput><tr>#dayData[thisDate][y]#</tr></cfoutput>
</cfloop>
<cfelse>
<cfoutput><tr><td colspan="4">There are no classes scheduled for this day</td></tr></cfoutput>
</cfif>
<cfelse>
<cfoutput><tr><td colspan="4">Schedule not available</td></tr></cfoutput>
</cfif>
</cfloop>
What I'm trying to do is dynamically insert an anchor (e.g., < a name="anchor">) depending on whether the date in the displayed row is the current date. So I want a cfif that will display the anchor on the row of the current day in the schedule. The goal is to link to this using the anchor.
Any suggestions are much appreciated.
So something like this?
<cfif dateFormat(now()),"yyyy-mm-dd") eq thisDate>
<a name="anchor" />
</cfif>
BTW, did you know you can loop over dates in ColdFusion.
<cfloop from="#theDate#" to="#dateAdd("d", 6, theDate)#" index="thisDate" step="#CreateTimeSpan( 1, 0, 0, 0 )#">
</cfloop>
This assumes that the dates are in a format that CF sees as dates, like what now() returns.

Evaluate List of ColdFusion Variables

i created a table using coldfusion lists system, rows are months and the columns are cities, and the variable is the sales, now i need to add to every evaluated sale variable, the list of companies that made these sales, so far there is no problem with writting the sql of this table and the sales, but i cant define-#evaluate# the list of these companies that belongs to each sale in the table, since it is the LIST of companies, not one variable like sale, i hope i made myself clear :) here is my code:
The query with some sets:
<cfset month_list_name = "#message13#,#message2#,#message3#,#message4#,#message5#,#message6#,#message7#,#message8#,#message9#,#message10#,#message11#,#message12#">
<cfset type_index = 'NETTOTAL'>
<cfquery name="GET_CITY" datasource="#DSN#">
SELECT CITY_ID,CITY_NAME FROM SETUP_CITY ORDER BY CITY_NAME
</cfquery>
<cfquery name="GET_COMPANY" datasource="#DSN#">
SELECT COMPANY_ID,FULLNAME FROM COMPANY ORDER BY COMPANY_ID
</cfquery>
<cfset c_index = 'COMPANY_ID'>
<cfset no_cities = 0 />
<cfset cities_list = ''>
<cfset cities_list_name = ''>
<cfset month_list = ''>
<cfoutput query="get_city"><cfset cities_list = listappend(cities_list,city_id)><cfset no_cities ++ /></cfoutput>
<cfoutput query="get_city"><cfset cities_list_name = listappend(cities_list,city_name)></cfoutput>
<cfloop from="1" to="12" index="i"><cfset month_list=listappend(month_list,i)></cfloop>
<cfquery name="GET_SALES_TOTAL" datasource="#dsn#">
SELECT
SUM(NETTOTAL) NETTOTAL,
SC.CITY_ID,
DATEPART(MM,INVOICE_DATE) AY,
C.FULLNAME,
C.COMPANY_ID AS COMPANY_ID
FROM
#DSN2_ALIAS#.INVOICE I,
SETUP_CITY SC,
COMPANY C
WHERE
MONTH(INVOICE_DATE) >= 1
AND MONTH(INVOICE_DATE) < 12
AND C.COMPANY_ID=I.COMPANY_ID
AND SC.CITY_ID=C.CITY
AND PURCHASE_SALES=1
GROUP BY
DATEPART(MM,INVOICE_DATE),
SC.CITY_ID,
C.FULLNAME,
C.COMPANY_ID
</cfquery>
here is the definitions for lists:
<cfloop list="#month_list#" index="month_index">
<cfloop list="#cities_list#" index="city_index">
<cfoutput query="GET_SALES_TOTAL">
<cfif city_id eq city_index and AY eq month_index>
<cfloop list="#type_index#" index="tt_index">
<cfset 'alan_#tt_index#_#city_index#_#month_index#' = evaluate(tt_index)>
<cfset 'alan2_#city_index#_#tt_index#_#month_index#' = evaluate(tt_index)>
</cfloop>
<cfloop list="#c_index#" index="cc_index">
<cfif cc_index eq company_id>
<cfset 'company_#cc_index#_#city_index#_#month_index#' = evaluate(cc_index)>
</cfif>
</cfloop>
</cfif>
</cfoutput>
</cfloop>
</cfloop>
and here's the table:
<cfloop list="#month_list#" index="m_index">
<tr class="color-row" height="20">
<td width="150" nowrap><b><cfoutput>#left(listgetat(month_list_name,listfind(month_list,m_index)),20)#</cfoutput></b></td>
<cfloop list="#cities_list#" index="ddd_other">
<cfloop list="#type_index#" index="ii_index">
<td align="center">
<cfif isdefined('alan_#ii_index#_#ddd_other#_#m_index#') and len(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'))>
<cfset all_toplam=evaluate('alan_#ii_index#_#ddd_other#_#m_index#')+all_toplam>
<cfset 'total_#ii_index#_#m_index#'=evaluate('total_#ii_index#_#m_index#') + #evaluate('alan_#ii_index#_#ddd_other#_#m_index#')#>
<cfset 'total2_#ddd_other#'=evaluate('total2_#ddd_other#') + #evaluate('alan2_#ddd_other#_#ii_index#_#m_index#')#>
<cfif listfindnocase('NETTOTAL',ii_index)>
<cfif isdefined('attributes.money') and attributes.money is 2>
<cfoutput>#TLFormat((evaluate('alan_#ii_index#_#ddd_other#_#m_index#')/get_money.rate2),2)#</cfoutput>
<cfelse>
<cfoutput>#TLFormat(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'),2)#</cfoutput>
</cfif>
<cfelse>
<cfif isdefined('attributes.money') and attributes.money is 2>
<cfoutput>#TLFormat((evaluate('alan_#ii_index#_#ddd_other#_#m_index#')/get_money.rate2),2)#</cfoutput>
<cfelse>
<cfoutput>#TLFormat(evaluate('alan_#ii_index#_#ddd_other#_#m_index#'),2)#</cfoutput>
</cfif>
</cfif>
<cfelse>
0
</cfif>
<cfloop list="#c_index#" index="co_index">
<cfif listfindnocase('COMPANY_ID',co_index)>
<cfoutput>#evaluate('company_#co_index#_#ddd_other#_#m_index#')#</cfoutput>
</cfif>
</cfloop>
</td>
</cfloop>
</cfloop>
<cfloop list="#type_index#" index="kk_ind">
<td align="center" class="txtbold">
<cfif isdefined('attributes.money') and attributes.money is 2>
<cfoutput>#TLFormat((evaluate('total_#kk_ind#_#m_index#')/get_money.rate2),2)#</cfoutput>
<cfelse>
<cfoutput>#TLFormat(evaluate('total_#kk_ind#_#m_index#'),2)#</cfoutput>
</cfif>
</td>
</cfloop>
</tr>
</cfloop>
it says: Variable company_COMPANY_ID_1_1 is undefined. where is my mistake?
i cant evaluate the LIST of company_idies in the same way i evaluated the NETTOTAL, since net total is not the list, but only one value... And thank you all for the help and attention! ;)
There are a lot of things you can improve in this code.
Incrementing the number of cities inside a loop is unnecessary. You can get the count directly from SQL (select count(*) from setup_city) or from CF after building your list (listlen(cities_list).
For that matter, it's unnecessary to loop through the list manually when you can simply use ValueList to have CF do the work for you:
cities_list = ValueList( get_city.city_id );
cities_list_name = ValueList( get_city.city_name );
There's no reason to build a list of months if all you're ever going to do is loop from 1 to 12. Use the 1-to-12 loop instead.
type_index doesn't need to be a list if you only have one item. Use the variable instead. Replace it with a list only when you actually need a list; this will also remove the need to use evaluate, which is making your code hard to read and difficult for people to follow. Same thing for cc_index.
Because scopes can be accessed as if they were structs, it's generally better (and can be significantly faster) to use structkeyexists( attributes, "money" ) rather than isdefined( "attributes.money" ). Specifying the scope for isdefined does help, of course ... isdefined( "money" ) is not only slow, but could return a completely different variable if you're not careful.
Speaking of structs, I would definitely recommend that you use them rather than concatenated variable names. It's extremely difficult to parse what you have written in order to understand what you are trying to do: using structs would make it easier to follow.
Finally, it looks to me like you're doing a ton of work in ColdFusion to do something that your database could do for you much more easily. If you need grand totals for companies by city, why not just do this?
<cfquery name="GET_SALES_GRAND_TOTAL" datasource="#dsn#">
SELECT
SUM(NETTOTAL) NETTOTAL,
SC.CITY_ID,
C.FULLNAME,
C.COMPANY_ID AS COMPANY_ID
FROM
#DSN2_ALIAS#.INVOICE I,
SETUP_CITY SC,
COMPANY C
WHERE
MONTH(INVOICE_DATE) >= 1
AND MONTH(INVOICE_DATE) < 12
AND C.COMPANY_ID=I.COMPANY_ID
AND SC.CITY_ID=C.CITY
AND PURCHASE_SALES=1
GROUP BY
SC.CITY_ID,
C.FULLNAME,
C.COMPANY_ID
</cfquery>
Is there a need to check that the month is between 1 and 12? If INVOICE_DATE exists, then it has to have a month between 1 and 12. I would either remove those lines from the query entirely or replace them with INVOICE_DATE IS NOT NULL.
You should then be able to get your table of results with something that looks roughly like this:
<cfoutput query="GET_SALES_TOTAL" group="AY">
<!--- months first --->
<tr>
<!--- month-specific stuff goes here --->
<td>month_header</td>
<cfoutput group="city_id">
<!--- city-specific stuff --->
<td><!--- format NETTOTAL however you want here ---></td>
</cfoutput>
</tr>
</cfoutput>
<tr>
<td>grand_total_header</td>
<cfoutput query="GET_SALES_GRAND_TOTAL" group="city_id">
<td><!--- format NETTOTAL here ---></td>
</cfoutput>
</tr>
Hopefully this is of some help to you. I would strongly encourage you to read up on arrays and structs; someone using evaluate is typically doing it the hard way when there is a much easier way of managing things, and there are plenty of examples both on SO and elsewhere of CF code that you should be able to learn from.