jQuery Sortable update com PHP + MySQL
Olá,
Estou usando jQuery Sortable (https://johnny.github.io/jquery-sortable) - em especial o exemplo: Serialization and delay,
Porém, estou tendo problemas para atualizar a ordem e pai do item do menu no banco de dados (MySQL) usando PHP.
Seguem meus códigos:
JavaScript
...
$(function () {
var group = $("ol.serialization").sortable({
group: 'serialization',
delay: 500,
onDrop: function ($item, container, _super) {
var data2 = group.sortable("serialize").get();
$.ajax({
method: "POST",
url: "saveList.php",
data: {
list: data2
}
}).done(function( data ) {
alert( "Data Loaded: " + data );
});
}
});
});
…
**saveList.php**
<?php
include('connection2.php');
$conn = new mysqli(HOST, USER, PASSWORD,DATABASE);
if ($conn->connect_error) {
exit;
}
if ($_POST) {
$array = $_POST['list'];
saveList($conn,$array);
exit;
}
function saveList($conn, $list, $id_menupai = 0, &$ordem = 0) {
foreach($list as $item):
$ordem++;
$sql = "UPDATE menu SET id_menupai = ?, ordem = ? WHERE id = ?";
$statement = $conn->prepare($sql);
$statement->bind_param('iii', $id_menupai, $ordem, $item["id"]);
$statement->execute();
if (array_key_exists("children", $item)):
saveList($conn, $item["children"], $item["id"], $ordem);
endif;
endforeach;
}
?>
No entanto, não está fazendo o update no banco de dados.
Parece que o foreach não está lendo o array.
Por favor, alguém pode me ajudar?
Obrigado
EzequielDiscussão (1)
Carregando comentários...