View Full Version : Updating an array

12-14-2011, 05:33 PM
Any chance anyone could give me a pointer on how to update the following?

I have a string of my data in one row of a table:



I also have 5 variables I've created. I need to update the Price of each of these strings with a var based on the season id.

So update Price with $high where Season = 1

So update Price with $mid where Season = 2

So on and so on. I can work this type of thing out when I have the dat in separate rows but not when all of the data is in one array like this.

Make sense?


12-14-2011, 08:56 PM
Redesign your table.
Any time you store multiple values in a table you are violating first normal form of database normalization.

Old Pedant
12-14-2011, 09:03 PM
Opinion? You *SHOULD* have the "dat[a] in separate rows."

You should *NEVER* have a delimited list of values in a single field in *ANY* relational database.

You really should redesign this database and follow proper design principles.

If this is somebody else's design, go whack them up side the head and make them change it. If it's your boss's design, and you are stuck with it...

You *could* do this. Just put all the data in one string and then split the string on the semicolons. Then loop through the array, processing 3 elements at a time. Then use logic to change the proper element of this now-15-element array. When finishied, JOIN the array back into a single string (using semicolon as separator) and finally update the DB. That's a truly crappy solution compared to doing it right with a separate table. But it could work.

12-15-2011, 09:17 AM
I hear you loud and clear and understand, but its not my code and with limited knowledge and time I have ti use what I can get my hands on.

Thanks for the replies.


Old Pedant
12-15-2011, 10:59 PM
Okay, so do what I said. With minor mod.

Read the string into PHP.
Split it on the commas.

Process it by looping through the resultant array.
For each element of the array, split on ";;" to create an inner array.
If the last element of this inner array (which should be the Season) matches the value(s) you are looking for, then change the next-to-last element of the inner array (which should be the Price). Re-join the inner array and put it back in the outer array.
When the loop is done, rejoin the outer array with a comma and then, finally, go UPDATE the record in the table.

It's going to be really slow and really tedious, but it should work just fine.

01-13-2012, 02:21 PM
Sorted thank you.