0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, that is what I did the only difference is I created a session variable and made this my structure but was definitely the way to go. Now just going to loop the struct and insert the key and the value into my table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.