首页 > 解决方案 > 如果我从 DB 更改时间列表的顺序,我是否应该更改 DB 中的 ID

问题描述

我列出了任务。每个人的任务在数据库中都有 ID。Id 列已设置自动增量。

我通过 jquery ui 中的 sortable() 函数通过 jquery 对这个列表进行排序。

现在,如果我更改时间顺序,我会更改 DB 中的 ID 顺序。但是当 Id 的值相同时,更新的 bcz 不起作用 - 我需要从 200 更改一次排序,从 0 更改第二次排序。这是一个好的解决方案吗?Ans 是否可以更改 Id 或 shoula 我为此目的创建另一个列?

谢谢

$i = 0;

// load all data from db in ascending order bcz of find first id value
$first_index = $database -> select (
    'items', // table
    'id', // column
    [ 'ORDER' => ['id' => 'ASC' ] ]// ordering
    );

// if first id value is equal to 0 then set up i as 1000 bcz if there are same values UPDATE table below will not work
if ( $first_index[0] == 0 ) {
    $i = 200;
} else if ( $first_index[0] == 200  ) {
    $i = 0;
};

// convert array (item[]=1&item[]=2) to separate items
foreach ($_POST['item'] as $value) {
    // Execute statement:
    $affected = $database -> update ('items',
    [ 'id' => $i ], // SET (set id as $i which is increasing for each value)
    [ 'id' => $value ] // WHERE
    );
$i++;
}

标签: phpmedoo

解决方案


推荐阅读