PHP/MySQL在数据库中交换位置+;JavaScript(jQuery)

PHP/MySQL在数据库中交换位置+;JavaScript(jQuery),php,jquery,mysql,Php,Jquery,Mysql,我目前正在开发一个网站,使用PHP和jQuery在MySQL数据库中存储书签 用于书签的MySQL如下所示(CSV格式): 我正在使用jquerysortable移动书签(类似于googlechrome)。下面是我用来格式化书签并将数据发布到PHP页面的JavaScript代码: $(".bookmarks").sortable({scroll: false, update: function(event, ui){ // Update bookmark position in

我目前正在开发一个网站,使用PHP和jQuery在MySQL数据库中存储书签

用于书签的MySQL如下所示(CSV格式):

我正在使用jquerysortable移动书签(类似于googlechrome)。下面是我用来格式化书签并将数据发布到PHP页面的JavaScript代码:

$(".bookmarks").sortable({scroll: false, update: function(event, ui){
        // Update bookmark position in the database when the bookmark is dropped
        var newItems = $("ul.bookmarks").sortable('toArray');
        console.log(newItems);
        var oldItems = "";
        for(var imgI=0;imgI < newItems.length;imgI++) {
            oldItems += $("ul.bookmarks li#" + imgI + " img").attr("id") + ",";
        }
        oldItems = oldItems.slice(0, oldItems.length-1);
        console.log("New position: " + newItems);
        console.log("Old position: " + oldItems);

        // Post the data 
        $.post('inc/updateBookmarks.php', 'update=true&olditems=' + oldItems + "&newitems=" + newItems, function(r) {
            console.log(r);
        });
    }
});
每个索引等于链接计数位置,结果更新将成为:

Array
(
    [0] => 1
    [1] => 0
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 2
)
我尝试了很多方法,但没有一种是成功的


提前感谢。

到目前为止,我已经设法发现我将每个li.droplet的img ID属性设置为错误的值,他们现在设置了匹配的链接计数(从数据库)

我还意识到,例如,如果我们的书签按顺序开始:
0,1,2,3,4,5

我们将书签0拖动到位置1,因此新的输出是,
1,0,2,3,4,5

(新)PHP的输出:

//将旧位置更新为新位置

对于($anID=0;$anID),经过长时间的谷歌搜索、搜索和测试,我找到了一些实用的东西,但不是100%

// Update the old place to the new one
for($anID=0;$anID<count($arrOldItems);$anID++) {
    if($oldCycle != $arrNewItems[$anID]) {
        if($arrOldItems[$anID] != $arrNewItems[$anID]) {
            $sQuery = "UPDATE linkz AS rule1 JOIN linkz AS rule2 ON (rule1.link_count = $arrOldItems[$anID] AND rule2.link_count = $arrNewItems[$anID]) OR ( rule1.link_count = $arrNewItems[$anID] AND rule2.link_count = $arrOldItems[$anID]) SET rule1.link_count = rule2.link_count, rule2.link_count = rule1.link_count";
            mysql_query($sQuery) or die(mysql_error());
            // Now set a temporary variable which we'll check later
            $oldCycle = $anID;
        }
    }else{
        $oldCycle = NULL;
    }
}
//将旧位置更新为新位置
对于($anID=0;$anID)
Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)
Array
(
    [0] => 1
    [1] => 0
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 2
)
// Update the old place to the new one
for($anID=0;$anID<count($arrOldItems);$anID++) {            
    echo $arrNewItems[$anID] . " = " . $arrOldItems[$anID] . "\n";
    echo $arrOldItems[$anID] . " = " . $arrNewItems[$anID] . "\n\n";

    //mysql_query("UPDATE linkz SET link_count='" . $arrNewItems[$anID] . "' WHERE userid='" . $usrID . "' AND link_count='" . $arrOldItems[$anID] . "'") or die(mysql_error());

    //mysql_query("UPDATE linkz SET link_count='" . $arrOldItems[$anID] . "' WHERE userid='" . $usrID . "' AND link_count='" . $arrNewItems[$anID] . "'") or die(mysql_error());
}
// Update the old place to the new one
for($anID=0;$anID<count($arrOldItems);$anID++) {
    if($oldCycle != $arrNewItems[$anID]) {
        if($arrOldItems[$anID] != $arrNewItems[$anID]) {
            $sQuery = "UPDATE linkz AS rule1 JOIN linkz AS rule2 ON (rule1.link_count = $arrOldItems[$anID] AND rule2.link_count = $arrNewItems[$anID]) OR ( rule1.link_count = $arrNewItems[$anID] AND rule2.link_count = $arrOldItems[$anID]) SET rule1.link_count = rule2.link_count, rule2.link_count = rule1.link_count";
            mysql_query($sQuery) or die(mysql_error());
            // Now set a temporary variable which we'll check later
            $oldCycle = $anID;
        }
    }else{
        $oldCycle = NULL;
    }
}