Im trying to use this https://gist.github.com/linssen/2773872 on my php work but that code only shows a array text and doesnt update in my database.
Main page:
<ul id="test-list">
<?php do { ?>
<li id="listItem_<?php echo $row_dados['id']; ?>"><img src="images/arrow.png" alt="move" width="16" height="16" class="handle" /><strong><?php echo $row_dados['posicao']; ?></strong></li>
<?php } while ($row_dados = mysql_fetch_assoc($dados)); ?>
</ul>
This is creating a loop to get all the records from database and show it.
Script:
<script type="text/javascript">
// When the document is ready set up our sortable with it's inherant function(s)
$(document).ready(function() {
$("#test-list").sortable({
handle : '.handle',
update : function () {
var order = $('#test-list').sortable('serialize');
$("#info").load("process-sortable.php?"+order);
}
});
});
</script>
process-sortable.php:
<?php
/* This is where you would inject your sql into the database
but we're just going to format it and send it back
*/
foreach ($_GET['listItem'] as $position => $item) :
$sql[] = "UPDATE `dados` SET `posicao` = $position WHERE `id` = $item";
endforeach;
print_r ($sql);
?>
But this only prints the result i would like to change this code to actually update my database.
The result:
Array
(
[0] => UPDATE `dados` SET `posicao` = 0 WHERE `id` = 4
[1] => UPDATE `dados` SET `posicao` = 1 WHERE `id` = 3
[2] => UPDATE `dados` SET `posicao` = 2 WHERE `id` = 1
[3] => UPDATE `dados` SET `posicao` = 3 WHERE `id` = 2
[4] => UPDATE `dados` SET `posicao` = 4 WHERE `id` = 5
[5] => UPDATE `dados` SET `posicao` = 5 WHERE `id` = 6
[6] => UPDATE `dados` SET `posicao` = 6 WHERE `id` = 7
[7] => UPDATE `dados` SET `posicao` = 7 WHERE `id` = 8
[8] => UPDATE `dados` SET `posicao` = 8 WHERE `id` = 9
[9] => UPDATE `dados` SET `posicao` = 9 WHERE `id` = 10
[10] => UPDATE `dados` SET `posicao` = 10 WHERE `id` = 277
[11] => UPDATE `dados` SET `posicao` = 11 WHERE `id` = 278
)
So its working fine, but doesnt change the database based on this lines.
So the probem is probably on process-sortable.php, so any help would be awesome.
Cumps.
Are that gist code are from yourself or you references? Btw, have you check database connection and where $item coming from?
You can set $item from $_POST / $_GET, sql select or define just before sql update are execute. Check my snippet below, I put comment so you can read it easier :