douzhong5902 2012-06-16 08:16
浏览 56

如何在php和mysql中更改db行位置并维护顺序

i have a php project i've been working on using Doctrine 1.2.4, mysql and Zend (not related to the issue). there has been a new requirement where from the admin panel, one can change the appearance of their team members on their team page by position.

So i added a position int column to the table. Now the main problem is to change the position and maintain the order. I've been scratching my head and found a workaround using arrays in php but it's buggy. here is my way: select position as key and id as value into an array, and work on the reordering on that array and reupdate the table based on the rearranged array. here is my code:

//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);

I thought it could have worked perfectly but after some tries the positions are getting mixed up. I'm wondering if there is out there someone who have solved this problem already. if Yes i little help would much appreciated

thanks for reading this

  • 写回答

1条回答 默认 最新

  • dqlm80253 2012-06-16 09:48
    关注
    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)
            );
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行