时间:2021-07-01 10:21:17 帮助过:9人阅读
/*前移*/
if($result=='up'){
$sql0=$db->query("select sort from ".$db->table('product')." where id='$id'");
$row=$db->fetch_array($sql0);
if(!$row){
$oldpx=1;
}else{
$oldpx=$row['sort'];
}
if($oldpx>1){
$db->query("update ".$db->table('product')." set sort=sort+1 where sort=".$oldpx-1);
$db->query("update ".$db->table('product')." set sort=sort-1 where id=".$id);
}
header("Location:product_list.php\n");
exit;
}
/*后移*/
if($result=='down')
{
$sql1=$db->query("select sort from ".$db->table('product')." where id='$id'");
$row=$db->fetch_array($sql1);
if(!$row){
$oldpx=9999;
}else{
$oldpx=$row['sort'];
}
$sql2=$db->query("select sort from ".$db->table('product')." order by sort desc limit 1");
$rows=$db->fetch_array($sql2);
if(!$rows){
$maxpx=9999;
}else{
$maxpx=$rows['sort'];
}
if($oldpx<$maxpx){
$db->query("update ".$db->table('product')." set sort=sort-1 where sort=".$oldpx+1);
$db->query("update ".$db->table('product')." set sort=sort+1 where id=".$id);
}
header("Location:product_list.php\n");
exit;
}
/*前移*/
if($result=='up'){
?$sql0=$db->query("select?sort?from?".$db->table('product')."?where?id='$id'");
?$row=$db->fetch_array($sql0);
?if(!$row){
??$oldpx=1;
?}else{
??$oldpx=$row['sort'];
?}
?if($oldpx>1){
?$db->query("update?".$db->table('product')."?set?sort=sort+1?where?sort=".$oldpx-1);
?$db->query("update?".$db->table('product')."?set?sort=sort-1?where?id=".$id);
?}
?header("Location:product_list.php\n");
?exit;
}
/*后移*/
if($result=='down')
{
??$sql1=$db->query("select?sort?from?".$db->table('product')."?where?id='$id'");
??$row=$db->fetch_array($sql1);
??if(!$row){
???$oldpx=9999;
??}else{
???$oldpx=$row['sort'];
??}
??$sql2=$db->query("select?sort?from?".$db->table('product')."?order?by?sort?desc?limit?1");
??$rows=$db->fetch_array($sql2);
??if(!$rows){
???$maxpx=9999;
??}else{
???$maxpx=$rows['sort'];
??}
??if($oldpx<$maxpx){
???$db->query("update?".$db->table('product')."?set?sort=sort-1?where?sort=".$oldpx+1);
???$db->query("update?".$db->table('product')."?set?sort=sort+1?where?id=".$id);
??}
??header("Location:product_list.php\n");
??exit;
}
if($_REQUEST["action"] == "up"){
uppx($_REQUEST['goods_id']);
}
elseif($_REQUEST["action"] == "down"){
downpx($_REQUEST['goods_id']);
}
function uppx($goods_id)
{
if(!$goods_id)
{
$goods_id=0;
}
$sql=$GLOBALS['db']->query("select px from ".$GLOBALS['db']->table('product')." where id=".$goods_id);
$rs=$GLOBALS['db']->fetch_array($sql);
if(!$rs)
{
$oldpx = 1;
}
else
{
$oldpx = $rs['px'];
}
if($oldpx>1){
$oldpx=$oldpx-1;
$GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px+1 where px=".$oldpx);
$GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px-1 where id=".$goods_id);
}
header("Location:product_list.php");
exit;
}
function downpx($goods_id)
{
if(!$goods_id)
{
$goods_id=0;
}
$sql=$GLOBALS['db']->query("select px from ".$GLOBALS['db']->table('product')." where id=".$goods_id);
$rs=$GLOBALS['db']->fetch_array($sql);
if(!$rs)
{
$oldpx=1;
}
else
{
$oldpx=$rs['px'];
}
//假如$oldpx=1的话
$sql1=$GLOBALS['db']->query("select * from ".$GLOBALS['db']->table('product')." order by px desc limit 1");
$row=$GLOBALS['db']->fetch_array($sql1);
if(!$row)
{
$maxpx=999;
}
else
{
$maxpx=$row['px'];
}
//到这里那么最大的$maxpx=4
if($oldpx<$maxpx){
$oldpx=$oldpx+1;
$GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px-1 where px=".$oldpx);
$GLOBALS['db']->query("update ".$GLOBALS['db']->table('product')." set px=px+1 where id=".$goods_id);
}
header("Location:product_list.php");
exit;
}