I am using jquery sortable and trying to save these items' positions into a mysql table's row named "position". I can't seem to get them to save/update correctly. Everything is working ok on the jquery side, just the php update function. Right now it is saving the position rows as 3, which I get why. I just can't seem to wrap my head around how to do this properly.
Here's what I have for the PHP:
$id = $_POST['id'];
$arr = $_POST['positions'];
foreach($arr as $r) {
$sql = " UPDATE items
SET position = '$r'
WHERE groupId = '$id' ";
}
Table Structure / desired output:
id | groupId | position
----------------------------
3 10 0
6 8 -
8 10 3
10 5 -
15 10 2
18 10 1
The jQuery file:
$("#items").sortable({
update: function() {
var invId = $("input[name='deleteId']").val();
var post = $(this).sortable('serialize');
$.ajax({
type: 'POST',
url: 'file.php',
data: { positions: post, id: invId },
dataType: 'JSON',
cache: false,
success: function(output) {
console.log('success');
},
error: function(output) {
console.log('fail ');
}
});
}
});
Thanks.