php – JQuery UI保存可排序列表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – JQuery UI保存可排序列表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2574字,纯文字阅读大概需要4分钟。
内容图文
![php – JQuery UI保存可排序列表](/upload/InfoBanner/zyjiaocheng/711/c8e21661971b45e9a67a0416b66bf14b.jpg)
我知道之前已经问过这个问题,但解决方案对我没有用.我正在尝试将新的项目顺序保存到数据库中.
我已经非常简化了它,但这是它的基本思想.我有一个嵌入了可排序列表的表单.
<form id="itemlist">
<ul id="itemsort">
<li id="Item_1">Item<input type="hidden" name="itemid[]" value="itemsRowID01"/></li>
<li id="Item_2">Item<input type="hidden" name="itemid[]" value="itemsRowID02"/></li>
<li id="Item_3">Item<input type="hidden" name="itemid[]" value="itemsRowID03"/></li>
<li id="Item_4">Item<input type="hidden" name="itemid[]" value="itemsRowID04"/></li>
</ul>
</form>
我有JQuery和JQuery UI Loaded,以下代码启用了可排序列表功能,并将项目ID和新排序顺序发布到php脚本. “editor”变量是一个在加载时设置的公共变量,它工作正常.排序工作正常但我重新排序列表时发布的新订单值似乎没有改变.
//sorting feature
$("#itemsort").live('hover', function() {
$("#itemsort").sortable({
opacity:.5,
update : function () {
var neworder = $('#itemsort').sortable('serialize');
var inputs = serializePost('#itemlist');
$.post("core/actions.php",{
'order': editor,
'inputs': inputs,
'neworder': neworder},function(){
alert("Order saved.", 1);
});
}
});
});
在actions.php上……
if(isset($_POST['order'])){
//set a variable for each post
$batchid = $_POST['inputs']['itemid'];
parse_str($_POST['neworder'], $neworder);
//count the number of entries to be ordered
$count = count($batchid);
//use the count to create an incremental loop for each item to be updated.
$i=0;
while ($i <= $count) {
$query ="UPDATE {$_POST['order']} SET order=$neworder[item][$i] WHERE id=$batchid[$i]";
++$i;
}
}
我不确定为什么每个项目的订单都不会改变.
有任何想法吗?
-L
解决方法:
$("#list").live('hover', function() {
$("#list").sortable({
update : function () {
var neworder = new Array();
$('#list li').each(function() {
//get the id
var id = $(this).attr("id");
//create an object
var obj = {};
//insert the id into the object
obj[] = id;
//push the object into the array
neworder.push(obj);
});
$.post("pagewhereyouuselist.php",{'neworder': neworder},function(data){});
}
});
});
然后在您的PHP文件中,或在此示例中“pagewhereyouuselist.php”
$neworderarray = $_POST['neworder'];
//loop through the list of ids and update your db
foreach($neworderarray as $order=>$id){
//you prob jave a connection already i just added this as an example
$con = mysql_connect("host","username","password");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE table SET order = {$order} WHERE id = {$id}");
mysql_close($con);
}
应该这样做
我没有测试它,因为它是一个示例连接.我实际使用的实际脚本更具体到我的程序这是一个简化版本来显示这个概念
内容总结
以上是互联网集市为您收集整理的php – JQuery UI保存可排序列表全部内容,希望文章能够帮你解决php – JQuery UI保存可排序列表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。