Heim  >  Artikel  >  Backend-Entwicklung  >  php文章排序后进行前移后移,写了语句没起作用,不知道哪出问题了?

php文章排序后进行前移后移,写了语句没起作用,不知道哪出问题了?

WBOY
WBOYOriginal
2016-06-23 13:42:15867Durchsuche

我对文章排序进行手动前移后移,但是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;	}

根据你那代码我自己完整的演示了一遍,实现你要的效果了

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn