我有这个列出表中行的 PHP 代码:
$sql="SELECT * from pages order by menu_order ASC ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
echo '<a href="edit_page.php?pagename='.$result["pagename"].'">'.$result["title"].'</a> - <a href="edit_page.php?do=up&page_order='.$result["menu_order"].'&seq='.$result["sequence"].'">Move Up</a> | <a href="edit_page.php?do=down&page_order='.$result["menu_order"].'&seq='.$result["sequence"].'">Move Down</a><br><br>';
}
他们有上下链接来改变顺序
使用此代码:
$page_order = $_GET['page_order'];
if ($_GET['do'] == 'up')
{
$sql="UPDATE pages SET menu_order = '$page_order' +1 WHERE sequence != '".$_GET["seq"]."' AND menu_order < '$page_order' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$sql="UPDATE pages SET menu_order = menu_order -1 WHERE sequence = '".$_GET["seq"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
}
elseif($_GET['do'] == 'down')
{
$sql="UPDATE pages SET menu_order = '$page_order' -1 WHERE sequence != '".$_GET["seq"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$sql="UPDATE pages SET menu_order = menu_order +1 WHERE sequence = '".$_GET["seq"]."' ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
}
但它不起作用 - 它随机更改数字,就像将它们全部更改为 0 或负数
正确执行此操作的最佳方法是什么?
编辑:新代码...
我已经尝试过这段代码,但它不能很好地工作 - 如果我将底行向右移动到顶部,它每次都会更改数字 -1 但最终会出现重复的订单号?
$current = $_GET["menu_order"];
$prev = $current-1;
$next = $current;
if($_GET['do'] == 'up')
{
$sql2="UPDATE pages SET menu_order = '".($prev)."' WHERE sequence = '".$_GET["seq"]."' ";
echo $sql2.'<br>';
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$sql2="UPDATE pages SET menu_order = '".($next)."' WHERE sequence = '".($_GET["seq"]-1)."' ";
echo $sql2;
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
}
elseif($_GET['do'] == 'down')
{
$sql2="UPDATE pages SET menu_order = '".($next)."' WHERE sequence = '".$_GET["seq"]."' ";
echo $sql2.'<br>';
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
$sql2="UPDATE pages SET menu_order = '".($prev)."' WHERE sequence = '".($_GET["seq"]+1)."' ";
echo $sql2;
$rs2=mysql_query($sql2,$conn) or die(mysql_error());
}