ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old November 22nd, 2004, 11:05 AM
NightVa NightVa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 67 NightVa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 31 m 33 sec
Reputation Power: 5
Three arrays but only one gets populated

Hi all,

I have a database table that has three records. Each record has date fields that help me create a record that equals one week.

I have a query that pulls the data from the table and puts it into a query object. Here's the query:

*************************
<cfquery name="qSessionClosures" datasource="#dataSource#">
SELECT
wksClosed.Afld_weeksClosed_ID, wksClosed.DTfld_weeksClosed_startDate, wksClosed.DTfld_weeksClosed_endDate, wksClosed.Nfld_weekClosed_listPosition, wksClosed.YNfld_weeksClosed_isActive, sessDate.Nfld_sessionDate_sessionNumber, sessDate.YNfld_sessionDate_isActive

FROM
tbl_cwt_weeksClosed AS wksClosed, tbl_cwt_sessionDate AS sessDate

WHERE
wksClosed.YNfld_weeksClosed_isActive = 1 AND sessDate.YNfld_sessionDate_isActive = 1

ORDER BY
wksClosed.DTfld_weeksClosed_startDate ASC
</cfquery>
****************************

I need to populate three arrays, one for each week so they can be used in all kinds of different ways. I'm trying to use "Nfld_weekClosed_listPosition" to uniquely identify each record and populate the corresponding array based on the list position number (which is 1,2 or 3).

Here's what my code looks like:

****************************
<cfset sessionNumber = #qSessionClosures.Nfld_sessionDate_sessionNumber#>

<!--- Test to see if each list position is being retrieved. --->
<cfloop query="qSessionClosures">
List Position:<cfoutput>#Nfld_weekClosed_listPosition# </cfoutput>
</cfloop>
<!--- END test for data retrieval. --->

<!--- BEGIN to populate the arrays. --->
<cfif listPosition EQ 1>
<cfset firstWeekClosed = ArrayNew(1)>
<cfset firstWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset firstWeekClosed[1][2]= #qSessionClosures.DTfld_weeksClosed_endDate#>
<cfset firstWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>

<cfif listPosition EQ 2>
<cfset secondWeekClosed = ArrayNew(1)>
<cfset secondWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset secondWeekClosed[1][2]= #qSessionClosures.wksClosed.DTfld_weeksClosed_endDate#>
<cfset secondWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>

<cfif listPosition EQ 3>
<cfset thirdWeekClosed = ArrayNew(1)>
<cfset thirdWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset thirdWeekClosed[1][2]= #qSessionClosures.wksClosed.DTfld_weeksClosed_endDate#>
<cfset thirdWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>
<!--- END population of arrays. --->
****************************

With this code ONLY the first array gets populated. The subsequent arrays do not even exist. Please shed some light on this for me. It's probably a really simple solution but I'm just too close tot he problem to see it.

Thanks for any help.

Nathan

Reply With Quote
  #2  
Old November 22nd, 2004, 11:14 AM
bocmaxima's Avatar
bocmaxima bocmaxima is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2004
Location: Tucson, Sonora
Posts: 1,316 bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 16 h 48 m 13 sec
Reputation Power: 22
Send a message via AIM to bocmaxima
I don't see anywhere in your code where you're looping your query. For that, you can use either <cfloop query= or <cfoutput query=
Also, I don't see where you're incrementing the listPosition variable, which seems to be your counter variable.
The first would explain why only one array is being populated though.

Reply With Quote
  #3  
Old November 22nd, 2004, 11:21 AM
NightVa NightVa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 67 NightVa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 31 m 33 sec
Reputation Power: 5
Thank you for your reply!

I actually do have a bit of code that loops:

<!--- Test to see if each list position is being retrieved. --->
<cfloop query="qSessionClosures">
List Position:<cfoutput>#Nfld_weekClosed_listPosition# </cfoutput>
</cfloop>
<!--- END test for data retrieval. --->

I just don't know how to use this to populate the arrays.

I like your suggestion to increment the list position variable. I don't know how to do that as my brain is pretty muddled right now. Can you give me a code snippet that might work?


Nathan

Reply With Quote
  #4  
Old November 22nd, 2004, 11:45 AM
bocmaxima's Avatar
bocmaxima bocmaxima is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Aug 2004
Location: Tucson, Sonora
Posts: 1,316 bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level)bocmaxima User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 16 h 48 m 13 sec
Reputation Power: 22
Send a message via AIM to bocmaxima
You have a loop, but it's not over where you're setting the arrays. If you just call to access the recordset without moving to the next record, it will just give you the first record.
Code:
<cfset sessionNumber = #qSessionClosures.Nfld_sessionDate_sessionNumber#>

<cfset listPosition = 0>
<!--- Test to see if each list position is being retrieved. --->
<cfoutput query="qSessionClosures">
List Position:#Nfld_weekClosed_listPosition#
<cfset listPosition = listPosition+1>
<!--- END test for data retrieval. --->

<!--- BEGIN to populate the arrays. --->
<cfif listPosition EQ 1>
<cfset firstWeekClosed = ArrayNew(1)>
<cfset firstWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset firstWeekClosed[1][2]= #qSessionClosures.DTfld_weeksClosed_endDate#>
<cfset firstWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>

<cfif listPosition EQ 2>
<cfset secondWeekClosed = ArrayNew(1)>
<cfset secondWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset secondWeekClosed[1][2]= #qSessionClosures.wksClosed.DTfld_weeksClosed_endDate#>
<cfset secondWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>

<cfif listPosition EQ 3>
<cfset thirdWeekClosed = ArrayNew(1)>
<cfset thirdWeekClosed[1][1]= #qSessionClosures.DTfld_weeksClosed_startDate#>
<cfset thirdWeekClosed[1][2]= #qSessionClosures.wksClosed.DTfld_weeksClosed_endDate#>
<cfset thirdWeekClosed[1][3]= #qSessionClosures.Nfld_weekClosed_listPosition#>
</cfif>
<!--- END population of arrays. --->
</cfoutput>

Also, you should remove those hash marks in your cfset statements because they're not necessary.
You also need to change your ArrayNew declarations to ArrayNew(2) since you're using two-dimensional arrays.

Reply With Quote
  #5  
Old November 22nd, 2004, 12:42 PM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
Everything bocmaxima said is spot on. There's several problems in your code but the big ones are not looping over the array set (which is why only the first array is being set), and the fact that you're building a 2D array but only creating a 1D array.

Finally, this is what I see: duplicate code. This should always be a red flag. You can do this in a much more concise way. Also, it doesn't look to me like you really want a 2 dimensional array at all, what you really want is an array of structures:

Code:
<cfset theArray = arrayNew(1) />
<cfloop query="qSessionClosures">
	<cfset arrayAppend( theArray, structNew() ) />
	<cfset structInsert( theArray[qSessionClosures.currentRow], 'startDate', qSessionClosures.DTfld_weeksClosed_startDate ) />
	<cfset structInsert( theArray[qSessionClosures.currentRow], 'endDate', qSessionClosures.DTfld_weeksClosed_endDate ) />
	<cfset structInsert( theArray[qSessionClosures.currentRow], 'listPosition', qSessionClosures.DTfld_weeksClosed_listPosition ) />
</cfloop>
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Reply With Quote
  #6  
Old November 23rd, 2004, 08:37 AM
NightVa NightVa is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 67 NightVa User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 31 m 33 sec
Reputation Power: 5
Reply

Thank you for your replies!

You've both made me a better CF developer and I'm grateful.
I'll implement those suggestions and I have no doubt they'll work.

Queation: Will I still be able to access one array seperate from the others? I may just want information on the first week and the third week at some point.

This was my reasoning in building three seperate arrays though I like the concise nature of your code snippet.

Nathan

Last edited by NightVa : November 23rd, 2004 at 08:40 AM. Reason: Expand on my answer

Reply With Quote
  #7  
Old November 23rd, 2004, 08:47 AM
kiteless kiteless is offline
Moderator
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,488 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 18 h 10 m 11 sec
Reputation Power: 42
Yes, you don't need three separate arrays, you'll have one array with 3 elements in it. So if you wanted to get the third element in the array and output it's start date, you'd do this:

#myArray[3].startDate#

You can always dump the whole variable to see it's contents as well:

<cfdump var="#myArray#">

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Three arrays but only one gets populated


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway