[英]Drag list item to new relative position and save to database
我正在尝试构建一个函数,允许我显示一组图片(已经按照数据库中的特定顺序保存),并允许用户将每个图片相对于其他图片拖动到新订单中,即时( ajax?)每当图片被放入新位置时保存在后台。
我的想法是设置我的数据库表,如下所示:
tablename:picturetable
字段和样本值
[pictureset],[picture_order]
“SET1”, “Pic1A.jpg | Pic1B.jpg | Pic1C.jpg”
“SET2”, “Pic2C.jpg | Pic2A.jpg | Pic3B.jpg”
...等等。
这样,如果我调用一条记录,使用php我可以:
$oldorder=explode("|", $row[pic_order]);
我可以用来显示一个数组(foreach($ oldorder),在某种容器div中回显一个可拖动的div)来显示当前顺序的图片。 每次将图片放入新位置时,我都可以:
$neworder=implode ("|", [picture names in divs according to their new positions])
并在后台(ajax?)有数据库记录做:
UPDATE picturetable SET picture_order=$neworder WHERE pictureset="Set2"
我找到了一些帮助我创建可拖动图片的脚本,甚至一个据说可以执行ajax保存的脚本......但我似乎无法让它超越拖动部分(ajax保存的东西实际上并不存在)似乎发生了或者如果它发生了,图片不会被新的订单破坏。
我关注的模型位于此处,
http://www.gregphoto.net/sortable/ (最后显示在页面底部)
http://www.gregphoto.net/index.php/2007/01/16/scriptaculous-sortables-with-ajax-callback/ (详细代码......但与上面的图片拖动不完全相同)
但我想知道是否有人可以帮助我将javascript(或评论)删除到最近的代码,以便我可以清楚地知道应该发生什么。
我觉得我已经非常接近能够做到这一点,但Javascript令我感到困惑:无论如何,在脚本中实际发生的事情都反映在我在页面上看到的内容(即回显变量或数组)正在改变或后台发生的$ sql语句?
我希望这不是一个挑剔的问题。
好的,我对我在以下网站找到的脚本进行了一些重大修改: http : //www.webresourcesdepot.com/dynamic-dragn-drop-with-jquery-and-php
并提出以下内容(需要两个文件,主.php文件和updateDB.php文件。
请注意我在原始问题中描述的数据库表/内容的结构:我为每个图片集创建一个记录行,主键是图片集的ID,以及所需顺序的图片名称列表,放在一个文本字段中,每个图片名称用“pipe”(“|”)字符分隔。
这个相同的模型很可能很容易修改,以处理其他东西,如报价,段落,链接,等等。
这是第一个文件模型:
<?php
$conn=mysqli_connect("localhost", "username", "password", "database_name") or die ("Could not connect:" . mysqli_error($conn));
$_POST[setID]="Set1"; //sample value
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Dynamic Drag'n Drop</title>
<script type="text/javascript" src="../jquery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui-1.10.2.custom.min.js"></script>
<style>
ul {
margin: 0;
}
#contentLeft {
float: left;
width: auto;
height: auto;
border: black solid 1px;
}
#contentLeft li {/* I want the pictures to look like floated tiles rather than a vertical top-bottom list. The jquery code seems to require that the items be presented as li rather than just images*/
white-space: nowrap;
float: left;
list-style: none;
margin: 0 0 4px 0;
padding: 10px;
background-color:#00CCCC;
border: #CCCCCC solid 1px;
color:#fff;
}
#contentRight {/* just a green box over on the right that shows what happened in the background after an item is moved */
float: right;
width: 260px;
padding:10px;
background-color:#336600;
color:#FFFFFF;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$(function() {
$("#contentLeft ul").sortable({ opacity: 0.6, cursor: 'move', update: function() {
var order = $(this).sortable("serialize") + '&action=updateRecordsListings&setID=<?php echo $setID;?>';
$.post("updateDB.php", order, function(theResponse){
$("#contentRight").html(theResponse);
});
}
});
});
});
</script>
</head>
<body>
<div id="contentLeft">
<ul>
<?php //go get a set of pictures from the database
$query = "SELECT * FROM picset_table where setID={$_POST[setID]}";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{$currentOrder=explode("|",$row[pics_ordered]);}
foreach($currentOrder as $pic) {//cycle through picture names and present each in a li (floated)
//$picfix is needed here because MY naming convention is "setid_n.jpg", and javascript seems to break all stings on the "_" character, which messes this up
//so,after the data passes into the updateDB.php process, the "~" gets re-replaced with the "_" to construct the SQL Update string.
$picfix=str_replace("_","~",$pic); //you may not need this if you don't have underscores in your picture names.
echo "<li id=\"recordsArray_$picfix\"><img src=\"photos/$pic\" height=\"100px\" /></li>";
}
?>
</ul>
</div>
<div id="contentRight">
<p>Array will be displayed in this box when any pictures are moved.</p>
<p> </p>
</div>
</body>
</html>
这是updateDB.php文件
<?php
$conn=mysqli_connect("localhost", "username", "password", "database_name") or die ("Could not connect:" . mysqli_error($conn));
$action = mysqli_real_escape_string($conn, $_POST['action']);
$updateRecordsArray = $_POST['recordsArray'];
if ($action == "updateRecordsListings") {
$neworder=implode("|", $updateRecordsArray);
$picUnfix=str_replace("~","_",$neworder); // puts the underscore back where it belongs
$query = "UPDATE picset_table SET pics_ordered='".$picUnfix."'
WHERE setID=$setID";
mysqli_query($conn, $query);
//echo "<b>$query</b><br />";
echo '<pre>';
print_r($updateRecordsArray); //thisappears in the green box
echo '</pre>';
}
?>
也许这对其他人有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.