我對文章排序進行手動前移後移,但是sql語句好像就沒起作用,哪位幫忙看看,我是實在看不出來了
/*前移*/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;}
回複討論(解決方案)
會不會是文章的sort並不是連續的,就前移來說你取sort-1作為調教來修改有可能沒修改到資料庫記錄,然後即使當前sort+1可能還是沒有比下個文章的sort大
/*前移*/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;}
紅色標註的那裡改成 where id='".$id."'");
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;}
根據你那代碼我自己完整的示範了一遍,實現你要的效果了