Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

I wish I understood Arrays

Contributor ,
Feb 22, 2007 Feb 22, 2007
I need to get my output into an array - but I don't understand how.
I have the following code:

<cfset currently = #DateFormat(createDate(year(now()),month(now()),day(now())),'yyyy-mm-dd')#>

<cfloop index="loopCount" from="1" to="6">
<cfset lastMonthYearX=#DateFormat(dateAdd('m',-#loopCount#,currently),'yyyy-mm-dd')#>
<cfset lastYearX=datePart('yyyy',lastMonthYearX)>
<cfset lastMonthX=NumberFormat(datePart('m',lastMonthYearX),09)>
<cfset daysX=daysInmonth(lastMonthYearX)>

<!--- COLLECT DATA AND SUMS FROM DATABASE BASED ON PASSED DATE --->
<cfquery name="Query3" datasource="erpdb01">
SELECT sum(t_delall) delT, sum(t_invall) invT, sum(t_bookall) bookT
FROM dbo.tbrsar310110
WHERE t_year='#lastYearX#' AND t_month='#lastMonthX#' AND t_day <= '#daysX#'
</cfquery>

<!--- GET MONTHLY GOAL AMOUNTS --->
<cfquery name="Query4" datasource="brayapps">
SELECT *
FROM goalGraph
WHERE t_year='#lastYearX#' AND t_month='#lastMonthX#'
</cfquery>

<!--- SET VALUES FOR EACH OF THE FOUR GRAPH COLUMNS --->
<cfset del=#round((query3.delT/query4.delgoal)*100)#>
<cfset inv=#round((query3.invT/query4.invgoal)*100)#>
<cfset book=#round((query3.bookT/query4.bookgoal)*100)#>
</cfloop>

I need to get an amount for del, inv, book in each of the last 6 months - BUT HOW?

I'm guessing this would be best as a 2D array?

Any help is appreciated
A link to a GOOD tutorial is also appreciated - I've read the documentation and looked at some simple tutorials but none seem really all that clear to me.
570
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007
Well, to use an array, you need to initialise it:

<cfset a = arrayNew(1)>

It's only one dimensional because the second "dimension" will be a struct
(see below)

Stick that line above your loop. Give the array a better name.

Then you have to POPULATE the array. This is an easy modification of your
existing code to set the individual variables:

<cfset a[loopCount] = structNew()> <!--- each element is a struct --->

<cfset a[loopCount].del=round((query3.delT/query4.delgoal)*100)>
<cfset a[loopCount].inv=round((query3.invT/query4.invgoal)*100)>
<cfset a[loopCount].book=round((query3.bookT/query4.bookgoal)*100)>

The reason why the second "dimension" is a struct rather than an array is
that arrays are intrinsically "ordered", and there ought to be some sense
of a numeric relationship between the elements. This is not the case here.


As for reading... I'm not sure what to suggest if the docs don't help you:
http://livedocs.adobe.com/coldfusion/7/htmldocs/00000951.htm (there are
several sequential pages).

There's this:
http://coldfusion.sys-con.com/read/279900.htm

which I found on Google my searching for "cfmx array tutorial".

--
Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 22, 2007 Feb 22, 2007
That gets me the results I want - now I just have to figure out how it works.
Thanks for your time, help and suggestions.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007
P.S.

Nothing to do with your array issue, I think Adam covered that fairly well.

But this line is a bit overly complex:
<cfset currently =
#DateFormat(createDate(year(now()),month(now()),day(now())),'yyyy-mm-dd')#>

I would write that as:
<cfset currently = dateFormat(now(),'yyyy-mm-dd')>

I don't see a need to process the dateTime object now() into pieces to
then process the pieces back into a dateTime object.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 22, 2007 Feb 22, 2007
Ian - You're probably right, I'll look into this shortly - thanks for the time and input.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 22, 2007 Feb 22, 2007
using a cfdump, I see all the data, but now how do I use it?
lets say I wanted to output the value for the third del - is that: #a3.del#
it tells me Element DEL is undefined in TRENDS1.
(I named my array and struct "trends)
my code now is:

<cfset currently = #DateFormat(createDate(year(now()),month(now()),day(now())),'yyyy-mm-dd')#>
<cfset trends = arrayNew(1)>

<cfloop index="loopCount" from="1" to="6">
<cfset lastMonthYearX=#DateFormat(dateAdd('m',-#loopCount#,currently),'yyyy-mm-dd')#>
<cfset lastYearX=datePart('yyyy',lastMonthYearX)>
<cfset lastMonthX=NumberFormat(datePart('m',lastMonthYearX),09)>
<cfset daysX=daysInmonth(lastMonthYearX)>

<!--- COLLECT DATA AND SUMS FROM DATABASE BASED ON PASSED DATE --->
<cfquery name="Query3" datasource="erpdb01">
SELECT sum(t_delall) delT, sum(t_invall) invT, sum(t_bookall) bookT
FROM dbo.tbrsar310110
WHERE t_year='#lastYearX#' AND t_month='#lastMonthX#' AND t_day <= '#daysX#'
</cfquery>

<!--- GET MONTHLY GOAL AMOUNTS --->
<cfquery name="Query4" datasource="brayapps">
SELECT *
FROM goalGraph
WHERE t_year='#lastYearX#' AND t_month='#lastMonthX#'
</cfquery>

<!--- SET VALUES FOR EACH OF THE THREE GRAPH COLUMNS --->
<cfset trends[loopCount]=structNew()> <!--- EACH ELEMENT IS A STRUCTURE --->
<cfset trends[loopCount].del=round((query3.delT/query4.delgoal)*100)>
<cfset trends[loopCount].inv=round((query3.invT/query4.invgoal)*100)>
<cfset trends[loopCount].book=round((query3.bookT/query4.bookgoal)*100)>
</cfloop>
<tr>
<td class="trendgraph" colspan="2">
<cfdump var="#trends#">
#trends1.del#


I will eventually be outputting each amount to a graph, so I need to be able to call on each one individually.

Thanks
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007
You uses brackets to define elements for arrays, you can also do this
for structures, but most people use dot notation for simple structure keys.

<cfoutput>
#trends[1].del#
OR
#trends[1]["del"]#
OR with a variable <cfset myKey = "del">
#trends[1][myKey]#
<cfoutput>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 22, 2007 Feb 22, 2007
LATEST
Well of course your right!

Everything is working great now - thanks everybody!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 22, 2007 Feb 22, 2007
I suggest going back to square 1. The fact that you have queries inside a loop is a bad sign. At best, it's inefficient.

Without using the word array, what is the task at hand? In other words, when somebody runs this particular page, what should they see?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources