#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    kerala, India
    Posts
    96
    Rep Power
    5

    I need to split and save data


    Dear Friends,

    I need to split and save data and update field of each id in table. Could you please help me to update data?

    Actual data is [{"x":0,"y":0,"width":1,"height":1},{"x":1,"y":0,"width":2,"height":10},{"x":6,"y":0,"width":2,"heigh t":9}]

    eg: table = gridtable, fields = id and position

    id 4, position {"x":0,"y":0,"width":1,"height":1}
    id 5, position {"x":1,"y":0,"width":2,"height":10}
    id 6, position {"x":6,"y":0,"width":2,"height":9}

    My code is given below

    PHP Code:
    if(isset($_POST['onchangeData']) == 'updateposition')
    {
     
    $position $_POST['positionData']; //[{"x":0,"y":0,"width":1,"height":1},{"x":1,"y":0,"width":2,"height":10},{"x":6,"y":0,"width":2,"height":9}]
     
    $select_id        "SELECT id FROM homegrid";
     
    $select_id_exec   mysql_query($select_id);
     while(
    $idFromDB   mysql_fetch_array($select_id_exec)) {
       
    $id $idFromDB['id'];
       
    //update query?
     
    }

    Regards
    Sarath
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,406
    Rep Power
    24
    As per your example, there is nothing in it defining ID's, unless that is a static set of items. But what you would be interested in as to separating your items is: PHP: json_decode - Manual

    P.S. I've heard of those ancient MySQL_ commands you're are using, but thought they we merely a thing of the past. For your safety, look into MySQLi or PDO before the old MySQL_ commands are removed from an upcoming PHP release.

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping out here!
    Last edited by Triple_Nothing; July 23rd, 2015 at 07:22 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    kerala, India
    Posts
    96
    Rep Power
    5
    I am not looking for decoding. I need to store data
    {"x":0,"y":0,"width":1,"height":1} for ID 1,
    {"x":1,"y":0,"width":2,"height":10} for ID 2 and
    {"x":6,"y":0,"width":2,"heigh t":9} for ID 3
    from actual data like [{"x":0,"y":0,"width":1,"height":1},{"x":1,"y":0,"width":2,"height":10},{"x":6,"y":0,"width":2,"he igh t":9}]
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,264
    Rep Power
    2039
    The data that you've got there is in JSON format. There's some good native PHP functions to deal with it:

    PHP: json_decode - Manual
    PHP: json_encode - Manual

    In your case you'd need a combination of the two.

    You could decode the incoming string to get an array, then loop through the array and encode each value back to JSON to store it.

    But, like Triple_Nothing said, there's no identifier on each line, so the only way that you'd be able to have some ID is if you create your own incrementing value or index. There's nothing in that data that makes each record identifiable by itself.

    Comments on this post

    • Will-O-The-Wisp agrees : Thanks for helping out here!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    kerala, India
    Posts
    96
    Rep Power
    5
    Originally Posted by Catacaustic
    The data that you've got there is in JSON format. There's some good native PHP functions to deal with it:

    PHP: json_decode - Manual
    PHP: json_encode - Manual

    In your case you'd need a combination of the two.

    You could decode the incoming string to get an array, then loop through the array and encode each value back to JSON to store it.

    But, like Triple_Nothing said, there's no identifier on each line, so the only way that you'd be able to have some ID is if you create your own incrementing value or index. There's nothing in that data that makes each record identifiable by itself.
    Thanks for your reply.

    Now my code is :
    //This is the data I am getting [{"x":1,"y":0,"width":2,"height":10},{"x":6,"y":0,"width":2,"height":9}]
    PHP Code:
    <?php
        $position              
    json_decode($_POST['positionData'], true);
        
    $select_id             "SELECT id FROM homegrid";
        
    $select_id_exec   mysql_query($select_id);
        
    //print_r($position[1]);
        //print_r($position[2]); 
        //getting result seperately
        
    foreach ($position as $entry) {
                
    $x $entry['x'];
                
    $y $entry['y'];
                
    $width $entry['width'];
                
    $height $entry['height'];
                
    $positionjson json_encode($entry);
                
    print_r($positionjson);
                while(
    $idFromDB mysql_fetch_assoc($select_id_exec)) {
                
    //print_r($idFromDB);
                //echo $update = "update homegrid set position = '$positionjson' WHERE id = '" . $idFromDB['id'] . "' ";
                //mysql_query($update);
            
    }
        }
    ?>
    The output of update query is "update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '7' update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '8' "

    that means result of last position of array is getting.

    How can i get result like "update homegrid set position = '{"x":1,"y":0,"width":2,"height":10}' WHERE id = '7' update homegrid set position = '{"x":6,"y":0,"width":2,"height":9}' WHERE id = '8' " ?

    My table structure is given below
    id position
    7 {"x":1,"y":0,"width":2,"height":10}
    8 {"x":6,"y":0,"width":2,"height":9}

    Could you please help me to solve this issue?

    Comments on this post

    • Will-O-The-Wisp agrees : Hello! Please place your code in [code] or [php] tags, as appropriate, in the future. I've added them for this post :)
    Last edited by Sarath_PHP; July 24th, 2015 at 04:21 AM.
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,406
    Rep Power
    24
    Everything is pretty much running as you desire, but since you are running the while() within your foreach(), every row is being updated each time. So, the prior values ARE being entered, but then overwritten in the next loop.
    Last edited by Triple_Nothing; July 24th, 2015 at 06:46 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.

IMN logo majestic logo threadwatch logo seochat tools logo