1

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
$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?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Sarath
  • 21
  • 7
  • Learn about [`var_dump();`](http://php.net/manual/en/function.var-dump.php) instead of `print_r();`. You'll get more info. – D4V1D Jul 24 '15 at 09:18
  • Shouldn't you have an `ORDER BY` option in your `SELECT` query? Otherwise, it can return IDs in any order, so you won't have any control over which ID gets which position. – Barmar Jul 24 '15 at 09:30
  • Your problem is that the first time through your `foreach` loop you are consuming all the results in your `$select_id_exec` result set. So the second time thorough the foreach loop there are no more results in the `$select_id_exec` result set – RiggsFolly Jul 24 '15 at 09:38
  • When you say _//This is the data I am getting....._ Do you mean that is what is being passed into the script in `$_POST['positionData']` – RiggsFolly Jul 24 '15 at 09:40
  • Someone is going to say it so it may as well be me. **Dont use the mysql_ extension** it has been deprecated for years now and in PHP7 it has gone forever! Future proof your code and start using the **MYSQLI or PDO** extensions instead. [See this](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) you are obviously just learning so now is the best time to do it. – RiggsFolly Jul 24 '15 at 09:47
  • @RiggsFolly $_POST['positionData'] is the data. This data sample is shown first line in my questions. – Sarath Jul 24 '15 at 10:02
  • Have you tried @DhinjuDivakaran answer, I think it should probably work – RiggsFolly Jul 24 '15 at 10:04
  • Just a note: This all does rather depend upon you entering 2 coordinates and there being only 2 rows in the homegrid table. Is that always guaranteed to be the case? – RiggsFolly Jul 24 '15 at 10:05

2 Answers2

0

Try this code you dont need two loops

<?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
$i = 0;
while($idFromDB = mysql_fetch_assoc($select_id_exec)) {
    $x = $position[$i]['x'];
    $y = $position[$i]['y'];
    $width = $position[$i]['width'];
    $height = $position[$i]['height'];
    $positionjson = json_encode($position[$i]);
    //print_r($idFromDB);
    $update = "update homegrid set position = '$positionjson' WHERE id = '" . $idFromDB['id'] . "' ";
    mysql_query($update);
    $i++;
}
?>
Dhinju Divakaran
  • 893
  • 1
  • 8
  • 11
-1

I have edited your 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) {
    $data = array();

    $data['x'] = $entry->x;
    $data['y'] = $entry->y;
    $data['width'] = $entry->width;
    $data['height'] = $entry->height;
    $positionjson = json_encode($data);
    //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);
      }
  }
  ?>
Dharmesh Goswami
  • 1,155
  • 2
  • 13
  • 30