【发布时间】: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
)
我尝试了很多方法,但都没有成功。
提前致谢。
【问题讨论】: