1

我正在尝试获取一个可排序的列表以将其顺序保存在我的数据库中。这是我到目前为止所得到的:

HTML:

<table>
<tr><th>ID:</th><th>Title</th></tr>
<tbody class="sorted_table">
<tr data-id="1"><td>3<td></td>Title 1</td></tr>
<tr data-id="2"><td>5<td></td>Title 2</td></tr>
</tbody>
<table>

JS:

var group = $(".sorted_table").sortable({
    containerSelector: 'tbody',
    itemSelector: 'tr',
    placeholder: '<tr class="placeholder"/>',
    group: 'sorted_table',
    onDrop: function (item, container, _super) {

    var sorted = [];
    $('tr').each(function () {
        sorted.push($(this).data('id'));
    });

    $.post('order-lessons.php', {data: sorted}, function(){});
    }, 
    serialize: function (parent, children, isContainer) {
    return isContainer ? children.join() : parent.text()
   },

  tolerance: 6,
  distance: 10
})

在 order-lessons.php 上

if (isset($_POST['data'])) {

// Yep!
$data = $_POST['data'];

// Explode the POST data into an array
$lesson_ids = explode('&', $data);

// The first record is empty - discard it
//unset($lesson_ids[0]);

// All records are to be reordered, so initialize the lesson_order var
$lesson_order = 1;

// Loop through the array
foreach ($lesson_ids as $lesson_id) {

    // Explode again
    $lesson_id = explode('=', $lesson_id);

    // And this time keep only what's on the right of the = sign (the lesson_id)
    $lesson_id = $lesson_id[1];

    // Here's the SQL statement
    $sql = "UPDATE lessons SET lesson_order = '" . $lesson_order . "' WHERE lesson_id = " . $lesson_id;

    // And here's the execution of the statement
     @mysqli_query ($dbc, $sql);

    // Increment the lesson_order var so the next lesson_order is 1 higher
    $lessons_order++;

}

die();

}

我想让它以某种方式发送数组中的 ID 顺序。只是不知道从这里去哪里。

4

0 回答 0