Ross Mark
Ross Mark

Reputation: 37

Coldfusion adding multiple rows - multi arrays and serialized data

So I have some data that needs added to my database, it is a 4 digit year and some text (limited to around 300 characheters.

<cfinput name=year_of_achievent /><cfinput class=text name=achievement />

The problem is these rows have to be added dynamically. For one user it may be their first year so have no achievements, this is fine. Tyipically the users will have between 2 and 6 years of achievements but it has to be open.

What is the best way to store this data locally and then in the database given that it has to be stored as pairs of data.

I am thiking 2D array but I do not know how to

(i) add/append a row to a 2d array, how would I add a 4th row to the array below? (ii) store in my table as serialized data in my table?

<cfset myArray[1][1] = '2010'>
<cfset myArray[1][2] = 'swam the english channel'>

<cfset myArray[2][1] = '2009'>
<cfset myArray[2][2] = 'Raised 1m for charity'>

<cfset myArray[3][1] = '2008'>
<cfset myArray[3][2] = 'ran NY marathon'>

Any ideas if there is a better way to approach this, if not can you supply some syntax for the gaps in my knowledge?

Thanks

Upvotes: 0

Views: 532

Answers (1)

duncan
duncan

Reputation: 31922

Assuming a user can only have 1 achievement per year, don't use a 2D array, use a struct.

<cfset stuAchievements = {}>

<cfset stuAchievements["2010"] = "swam the english channel">
<cfset stuAchievements["2009"] = "Raised 1m for charity">
<cfset stuAchievements["2008"] = "ran NY marathon">

Or in other words:

<cfset stuAchievements[form.year_of_achievement] = form.achievement>

For storing it, I'd normalise this into a table of its own, Achievements, with columns for ID, year, achievement and a foreign key linking it back to the user.

Upvotes: 2

Related Questions