我有一个表单,在此表单上,用户可以更改显示顺序。这反映在显示我们所有员工的页面上,我们希望能够控制他们出现的订单。
提交表单时,我想更改数据库中的其他显示顺序以反映更改。
一些示例数据:
当他们保存该表单时,除显示顺序外,所有内容都会更新到数据库。这是在一个单独的函数调用中处理的。
function updateDisplayOrder($eid, $display_order_old, $display_order)
{
if ($eid > 0)
{
if ($display_order != $display_order_old)
{
$result = db_query("SELECT * FROM {help_employees} WHERE active = 1 ORDER BY display_order");
while ($arr = db_fetch_array($result))
{
if ($display_order > $display_order_old)
{
if ($arr["display_order"] > $display_order_old && $arr["display_order"] <= $display_order)
{
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", array($arr["display_order"] - 1, $arr["eid"]));
}
}
else
{
if ($arr["display_order"] < $display_order_old && $arr["display_order"] >= $display_order)
{
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", array($arr["display_order"] + 1, $arr["eid"]));
}
}
}
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d", $display_order, $eid);
}
}
}
现在发生了什么:
答案 0 :(得分:0)
如果插入新员工:
db_query("UPDATE {help_employees} SET display_order = display_order + 1 WHERE display_order >= :display_order", $display_order);
否则,如果$ display_order&gt; $ display_order_old
db_query("UPDATE {help_employees} SET display_order = display_order + 1 WHERE display_order >= :display_order AND id <> :id", $display_order, $id)`;
否则
db_query("UPDATE {help_employees} SET display_order = display_order + 1 WHERE display_order >= :display_order AND display_order < :display_order_old AND id <> :id", $display_order, $display_order_old, $id);
其中$ display_order_old,$ display_order和$ id是当前修改后员工的值
答案 1 :(得分:0)
从当前位置移除您的商品
db_query("UPDATE {help_employees}
SET display_order = display_order - 1
WHERE eid != %d AND active = 1 AND display_order > %d",
$eid, $display_order_old);
为其新职位添加一些空间
db_query("UPDATE {help_employees}
SET display_order = display_order + 1
WHERE eid != %d AND active = 1 AND display_order >= %d",
$eid, $display_order);
修改它:
db_query("UPDATE {help_employees} SET display_order = %d WHERE eid = %d ",
$display_order, $eid);