如何在php和mysql中更改数据库行位置和维护顺序

如何在php和mysql中更改数据库行位置和维护顺序,php,mysql,algorithm,Php,Mysql,Algorithm,我有一个php项目,我一直在使用Doctrine 1.2.4、mysql和Zend(与此问题无关)进行工作。有一个新的要求,从管理面板,可以改变他们的团队成员在他们的团队页面上的位置外观 所以我在表中添加了一个positionint列。现在的主要问题是改变位置和维持秩序。我一直在挠头,找到了一个在php中使用数组的解决方法,但它有缺陷。我的方法是:选择position作为key,id作为value进入数组,然后对该数组进行重新排序,并根据重新排列的数组重新更新表。 这是我的密码: //here

我有一个php项目,我一直在使用Doctrine 1.2.4、mysql和Zend(与此问题无关)进行工作。有一个新的要求,从管理面板,可以改变他们的团队成员在他们的团队页面上的位置外观

所以我在表中添加了一个positionint列。现在的主要问题是改变位置和维持秩序。我一直在挠头,找到了一个在php中使用数组的解决方法,但它有缺陷。我的方法是:选择position作为key,id作为value进入数组,然后对该数组进行重新排序,并根据重新排列的数组重新更新表。 这是我的密码:

//here i use string as my id to be able to view the changes .  
//you can swap to this:
  $anarray = array("23", "12", "4", "6", "2");
//$anarray = array("car", "dog", "cow", "cup", "plane");

var_dump($anarray);

function preserveSort($oldposition, $newposition, $arraytosort) {
  // this assumes that there is no zero in position
  $oldposition--;$newposition--;
  $indice = $newposition - $oldposition;
  $tmpNewPosistionData = $arraytosort[$oldposition];
  if ($indice > 0) {

      for ($i = $oldposition; $i < $newposition; ++$i) {
          echo $i . "<br/>";
          $arraytosort[$i] = $arraytosort[$i + 1];
      }
  } else {
      for($i=$oldposition;$i >$newposition; $i--){
          echo $i."<br/>";
          $arraytosort[$i] = $arraytosort[$i-1];        
      }
  }
  $arraytosort[$newposition] = $tmpNewPosistionData;
  var_dump($arraytosort);
}

echo "<br/>";
echo "changing position 1 to 4 <br/>";
preserveSort(1, 4, $anarray);
//在这里,我使用字符串作为id来查看更改。
//您可以切换到以下位置:
$anarray=数组(“23”、“12”、“4”、“6”、“2”);
//$anarray=数组(“汽车”、“狗”、“牛”、“杯子”、“飞机”);
var_dump($anarray);
函数保留排序($oldposition、$newposition、$arraytosort){
//这假设位置上没有零
$oldposition--;$newposition--;
$indice=$newposition-$oldposition;
$tmpNewPosistionData=$arraytosort[$oldposition];
如果($indice>0){
对于($i=$oldposition;$i<$newposition;++$i){
echo$i.“
”; $arraytosort[$i]=$arraytosort[$i+1]; } }否则{ 对于($i=$oldposition;$i>$newposition;$i--){ echo$i.“
”; $arraytosort[$i]=$arraytosort[$i-1]; } } $arraytosort[$newposition]=$tmpnewPositionData; 变量转储($arraytosort); } 回声“
”; 回声“将位置1更改为4
”; 保存排序(1,4,$anarray);
我原以为它可以很好地工作,但经过一些尝试后,位置变得混乱了。我想知道是否有人已经解决了这个问题。 如果是的话,我将不胜感激

感谢阅读此

函数move_元素($input,$from,$to){//我建议将$input作为第一个参数,以匹配PHP数组_*API
function move_element($input, $from, $to) { // I suggest $input as first paramter, to match the PHP array_* API
    // TODO: make sure $from and $to are within $input bounds
    // Assuming numeric and sequential (i.e. no gaps) keys
    if ($from == $to) {
        return $input;
    } else if ($from < $to) {
        return array_merge(
            array_slice($input, 0, $from),
            array_slice($input, $from +1, $to - $from),
            array($input[$from]),
            array_slice($input, $to +1, count($input) - $to)
        );
    } else if ($from > $to) {
        return array_merge(
            array_slice($input, 0, $to),
            array($input[$from]),
            array_slice($input, $to, $from - $to),
            array_slice($input, $from +1, count($input) - $from)
        );
    }
}
//TODO:确保$from和$to在$input范围内 //假设数字键和顺序键(即无间隙) 如果($from==$to){ 返回$input; }其他条件(从<$to){ 返回数组合并( 数组_切片($input,0,$from), 数组_切片($input、$from+1、$to-$from), 数组($input[$from]), 数组\u切片($input,$to+1,count($input)-$to) ); }else if($from>$to){ 返回数组合并( 数组_切片($input,0,$to), 数组($input[$from]), 数组\u切片($input,$to,$from-$to), 数组\u切片($input,$from+1,count($input)-$from) ); } }