【问题标题】:PHP/MySQL Swap places in database + JavaScript (jQuery)PHP/MySQL 交换数据库中的位置 + JavaScript (jQuery)
【发布时间】:2010-03-13 16:38:42
【问题描述】:

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

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

id,userid,link_count,url,title,description,tags,shareid,fav,date
"1";"1";"0";"img/test/google.png";"Google";"Best. Search Engine. Ever.";"google, search, engine";"7nbsp";"0";"1267578934"
"2";"1";"1";"img/test/james-brooks.png";"jTutorials";"Best. jQuery Tutorials. Ever.";"jquery, jtutorials, tutorials";"8nbsp";"0";"1267578934"
"3";"1";"2";"img/test/benokshosting.png";"Benoks Hosting";"Cheap website hosting";"Benoks, Hosting, server, linux, cpanel";"9nbsp;";"0";"1267578934"
"4";"1";"3";"img/test/jbrooks.png";"James Brooks";"Personal website FTW!";"james, brooks, jbrooksuk, blog, personal, portfolio";"1nbsp";"0";"1267578934"
"6";"1";"4";"img/test/linkbase.png";"LinkBase";"Store and organise your bookmarks and access them from anywhere!";"linkbase, bookmarks, organisation";"3nbsp";"0";"1267578934"
"5";"1";"5";"img/test/jtutorials.png";"jTutorials";"jQuery tutorials, videos and examples!";"jquery, jtutorials, tutorials";"2nbsp";"0";"1267578934"

我正在使用 jQuery Sortable 来移动书签(类似于 Google Chrome 的做法)。这是我用来格式化书签并将数据发布到 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);
        });
    }
});

然后,PHP 页面会使用explode 拆分发布的数组,如下所示:

if(isset($pstUpdate)) {
    // Get the current and new positions
    $arrOldItems = $_POST['olditems'];
    $arrOldItems = explode(",", $arrOldItems);

    $arrNewItems = $_POST['newitems'];
    $arrNewItems = explode(",", $arrNewItems);

    // Get the user id
    $usrID = $U->user_field('id');

    // Update the old place to the new one
    for($anID=0;$anID<count($arrOldItems);$anID++) {
        //echo "UPDATE linkz SET link_count='" . $arrNewItems[$anID] . "' WHERE userid='" . $usrID . "' AND link_count='" . $arrOldItems[$anID] . "'\n";
        //echo "SELECT id FROM linkz WHERE link_id='".$arrOldItems[$anID]."' AND userid='".$usrID."'";

        $curLinkID = mysql_fetch_array(mysql_query("SELECT id FROM linkz WHERE link_count='".$arrOldItems[$anID]."' AND userid='".$usrID."'")) or die(mysql_error());
        echo $arrOldItems[$anID] . " => " . $arrNewItems[$anID] . " => " . $curLinkID['id'] . "\n";

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

        // Join a string with the new positions
        $outPos .= $arrNewItems[$anID] . "|";
    }

    echo substr($outPos, 0, strlen($outPost) - 1);
}

因此,每个书签都有自己的 link_count id(每个用户从 0 开始)。每次更改书签时,我都需要根据需要更改 link_count。如果我们把这个数组输出作为起点:

Array
(
    [0] => 0
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 4
    [5] => 5
)

每个索引等于link_count位置,结果更新会变成:

Array
(
    [0] => 1
    [1] => 0
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 2
)

我尝试了很多方法,但都没有成功。

提前致谢。

【问题讨论】:

    标签: php jquery mysql


    【解决方案1】:

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

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

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

    (新)PHP 的输出:

    // 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());
    }
    

    变成, 1 = 0, 0 = 1 0 = 1, 1 = 0

    2 = 2, 2 = 2

    3 = 3, 3 = 3

    4 = 4, 4 = 4

    5 = 5, 5 = 5 如果我们看一下粗体字,我们可以看到数据正在被再次覆盖,那么我该如何阻止这种情况发生呢?

    【讨论】:

      【解决方案2】:

      经过长时间的谷歌搜索、搜索和测试,我想出了一些实用的东西,但不是 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;
          }
      }
      

      它有点工作,但它不允许您交换相同的书签两次。我会想办法的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-28
        • 1970-01-01
        • 1970-01-01
        • 2012-06-18
        • 2017-02-27
        • 2016-10-04
        • 2012-04-15
        相关资源
        最近更新 更多