mysql—在php中,基于用户在前端管理(重新排序)的项更新db表字段的最佳方法

bvjveswy  于 2021-07-27  发布在  Java
关注(0)|答案(0)|浏览(181)

后端:我有一个名为item\u curation的表,它有字段(id,item\u id,rank)(mysql),它可以有数千条记录。
前端:我们有一个ui,其中所有项目都按页面列出,用户可以拖放以重新排列项目的顺序,也可以手动输入任何项目的排名。

e.g. A page has 60 items. User can rank item with rank 300 as rank 6.

用户重新排列或输入列组后,此项在项目管理表中进行

id = auto-inc,
item_id = id of the item (from the main item table),
rank = rank assigned to that item

我基本上需要一个代码,将重新排列的顺序,同时消除重复不留下差距,并保留用户输入的排名。php中有预定义的功能吗?或者有人做过类似的工作吗?
e、 g.如果用户输入的等级低于其当前等级,则给定等级的所有等级都必须递增,以便不存在重复的等级。

[I1 = 1, I2 = 2, I3 = 3, I4 = 4, I5 = 5]  => [I1 = 1, I2 = 2, I3 = 3, I4 = 2(changed), I5 = 5]  => [I1 = 1, I4 = 2, I2 = 3, I3 = 4, I5 = 5]

类似地,如果用户输入的等级高于其当前等级,则必须递减给定等级中的所有等级,以便不存在重复的等级。

[I1 = 1, I2 = 2, I3 = 3, I4 = 4, I5 = 5]  => [I1 = 1, I2 = 2, I3 = 5(changed), I4 = 4, I5 = 5]  => [I1 = 1, I2 = 2, I4 = 3, I5 = 4, I3 = 5]

我在实现中使用了array\ u splice,但是当同时进入较高的秩和较低的秩时,它就不起作用了。在foreach循环中,即使用户输入了秩,秩也会得到更新(必须优先)

User can not enter same rank for more than 1 item. However, user can enter a rank whose value is already assigned to another item.

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题