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?
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++;
}
?>
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);
}
}
?>