Ex
เพิ่มให้อีกแล้วกันนะสำหรับแก้ของเก่าให้อัพเดทลงฐานข้อมูลได้ Code (PHP)
table member {int id,int no,string name}
select name from member order by no asc
1 1 ไมเคิล
2 2 แม็ก
3 3 หมวย
4 4 หมี
<script type="text/javascript">
$(document).ready(function() {
$("#table-2").tableDnD({
onDragClass: "myDragClass",
onDrop: function(table, row) {
var rows = table.tBodies[0].rows;
var debugStr = "Row dropped was "+row.id+". New order: ";
for (var i=0; i<rows.length; i++) {
debugStr += rows[i].id+" ";
}
// $("#debugArea").html(debugStr);
//$("#debugArea").html($.tableDnD.serialize());
var order = $.tableDnD.serialize() + '&action=sortTable';
$.post("updateDB.php", order, function(theResponse){
$("#debugArea").html(theResponse);
});
},
onDragStart: function(table, row) {
$("#debugArea").html("Started dragging row "+row.id);
}
});
});
</script>
Code (updateDB.php)
#require("db.php");
$action = mysql_real_escape_string($_POST['action']);
$updateRecordsArray = $_POST['table-2'];
if ($action == "sortTable"){
$listingCounter = 1;
foreach ($updateRecordsArray as $recordIDValue) {
$query = "UPDATE table_test SET no = " . $listingCounter . " WHERE id = " . $recordIDValue;
//mysql_query($query) or die('Error,update query failed');
echo $listingCounter.": ".$query."<br>";
$listingCounter = $listingCounter + 1;
}
echo '<pre>';
print_r($updateRecordsArray);
echo '</pre>';
echo 'If you refresh the page, you will see that records will stay just as you modified.';
}