www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Updating an Array

  1. #1
    Join Date
    Dec 2011
    Posts
    14

    resolved [RESOLVED] Updating an Array

    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:

    2011-12-13;;1;;0;;3,2011-12-14;;1;;0;;1,2011-12-15;;1;;0;;5,2011-12-16;;1;;0;;1;

    (Year-Month-Day;;Availability;;Price;;Season,)

    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?

    Cheers
    Rich

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    How is that data string created? If it's some sort of application code serialization, it might be easier to do it there than in the SQL. Essentially: query that field from the DB, unserialize it, make the desired changes, serialize that, then update that DB row/field with the new string.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    14
    Its created through an Ajax script which I don't fully understand. Ideally I need to add this as an extra function.

  4. #4
    Join Date
    Dec 2005
    Posts
    2,984
    Firstly, it's a string, not an array. You have to process it to turn it into an array.

    I highly doubt you can update this using SQL alone (although you may be able to, but I doubt it's worth the effort).

    I'd pull out the string, modify it using a script in PHP or Perl or Ruby or whatever you're using and then stick it back in.

    I guess it goes without saying that storing data this way in a database is absurd and next to useless.

    PHP, as an example

    PHP Code:
    //example data
    $high '10';
    $mid '5';


    $string "2011-12-13;;1;;0;;3,2011-12-14;;1;;0;;1,2011-12-15;;1;;0;;5,2011-12-16;;1;;0;;1;";

    $updatedSets = array();
    $updatedString '';

    $sets explode(',',$string);
    foreach(
    $sets as $set) {
      
    $setArray explode(';;',$set);

      
    //$setArray[2] corresponds to the season
      //$setArray[1] corresponds to the price

      
    if($setArray[2] == 1) {
        
    $setArray[1] = $high;
      } elseif(
    $setArray[2] == 2) {
        
    $setArray[1] = $mid;
      }

      
    //store the updated string for each set in the array $updatedSets
      
    array_push($updatedSets,implode(';;',$setArray);
      
    }

    //implode the $updatedSets array with commas as the delimiter to obtain the new updated string
    $updatedString implode(',',$updatedSets);


    //do an sql update 
    I don't suppose you're open to the suggestion of rebuilding the database and storing the data properly in separate rows and columns?
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  5. #5
    Join Date
    Dec 2011
    Posts
    14
    I would but just wouldn't have time really at the moment.

    Thanks for this, I'll play around with it and try and send it back in.

  6. #6
    Join Date
    Dec 2005
    Posts
    2,984
    Quote Originally Posted by meadsy25 View Post
    I would but just wouldn't have time really at the moment.
    That's most everybody's reason for not spending a few hours to update something. It's fairly obvious though, that spending a few hours to normalize this database will probably save you tons of time in the future of having to write short scripts to deal with modifying parts of these long strings rather than short simple queries that will do it for you.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  7. #7
    Join Date
    Dec 2011
    Posts
    14
    Oh without a doubt. But I'm too much of a novice to update the Ajax script I have to accommodate that. Hence not enough time.

    Thank you so much for your help on this, it works a treat.

    Cheers
    Rich

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles