我正在使用PHP,我想创建一个包含Orderid,ID,Employee_Name和Employee_Salary行的表。我想要两个ID列,因为我试图让用户能够向上或向下移动表格行以更改表格行的顺序。显然,根据我的阅读,唯一的方法是使用ID列和Order ID列来操纵表行的顺序。
这是我试图用来上下移动行的代码。例如,我希望用户能够单击向上箭头按钮将第1行:Kelsey,第2行:最大值更改为第1行:最大值,第2行:Kelsey。
PHP代码:
<?php
// Variables -- Fill these out from the results of your page. (i.e. what item id to move up or down)
$id_item = 1; // ID of item you want to move up/down
$isUp = true; // Change to false if you want to move item down
// MySQL structure -- Fill these out to execute your queries without needing to update my code
$table_name = "employee"; // Name of table with your items in it
$col_position = "position"; // Name of column with position ID (Remember, this must be UNIQUE to all items)
$col_id = ""; // Name of column containing the items id (most likely the auto_incremented column)
if ($isUp)
{
$operator = "<";
$order = "DESC";
}
else
{
$operator = ">";
$order = "ASC";
}
// Get row we are moving
$request = mysql_query("
SELECT '.$col_position.', '.$col_id.' FROM '.$table_name.'
WHERE '.$col_id.' = '.$id_item.'
LIMIT 1");
// Save data for row we are moving
if(mysql_num_rows($request) > 0) {
$isPos1 = true;
while($row = mysql_fetch_assoc($request)) {
$position1 = $row[$col_position];
$id_item1 = $row[$col_id];
}
}
// Get row we want to swap with
$request2 = mysql_query("
SELECT '.$col_position.', '.$col_id.' FROM '.$table_name.'
WHERE '.$col_position.' '.$operator.' '.$position1.'
ORDER BY '.$col_position.' '.$order.' LIMIT 1");
// Save data from row we want to swap with
if(mysql_num_rows($request2) > 0) {
$isPos2 = true;
while($row = mysql_fetch_assoc($request2)) {
$position2 = $row[$col_position];
$id_item2 = $row[$col_id];
}
}
// If both rows exist (indicating not top or bottom row), continue
if ($isPos1 && $isPos2)
{
$query_update = mysql_query("
UPDATE '.$table_name.'
SET '.$col_position.' = '.$position2.'
WHERE '.$col_id.' = '.$id_item1.'");
$query_update2 = mysql_query("
UPDATE '.$table_name.'
SET '.$col_position.' = '.$position1.'
WHERE '.$col_id.' = '.$id_item2.'");
}
?>
答案 0 :(得分:1)
更新订单的查询应该类似于这样,只是为修改后的项目使用ID为5,新订单ID为2,为了示例:
UPDATE table SET orderID=2 WHERE ID=5;
UPDATE table SET orderID=orderID+1 WHERE ID!=5 AND orderID >= 2;
抱歉,我没有时间专门针对您的桌面设置进行定制,但这肯定会让您走上正确的轨道。