Home > Backend Development > PHP Tutorial > PHP interface implements drag and drop sorting function

PHP interface implements drag and drop sorting function

不言
Release: 2023-03-24 17:42:01
Original
2067 people have browsed it

This article mainly introduces the drag-and-drop sorting function of the PHP interface. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

List drag-and-drop sorting is a very common method. Function is often used in back-end interface processing. Today, the editor brings you a php interface to implement drag-and-drop sorting function. Friends who are interested should take a look.

List drag-and-drop sorting is a very Common functions, but how to deal with the back-end interface is a tangled problem

How to achieve the highest efficiency?

Let’s analyze a scenario first. Suppose there is a page with ten pieces of data. The so-called dragging means dragging the ten pieces of data back and forth, but each drag will affect other data. For example, dragging the last piece to the front. Then the next nine bars will automatically move back, and vice versa, um~~~

First imagine that the sorting number is fixed, just like there are ten chairs, and each chair is fixed there. Move The ones on the top are the people above, so it won’t affect the data on other pages, and everyone changes the table and chair numbers of other people before, so you don’t have to think about how much you need to add to rank.

Interface design:


##

//$ids 这十条数据的id集合,逗号隔开的字符串
//$oldIndex 原始位置,从0开始算
//$newIndex 要拖动的位置
function dragSort($ids,$oldIndex,$newIndex)
{
  //保证查找出来的数据跟前台提交的顺序一致,这里要order by field
  //id 主键 sort 排序值
  $sql = "select id,sort from 表名字 where id in ($ids) order by field(id, " . $ids . ") ";
  $list = "这里省略,就是去数据库找嘛";
  //id集合
  $idArr  = [];
  //排序集合
  $sortArr = [];
  foreach ($list as $item) {
    $idArr[]  = $item['id'];
    $sortArr[] = $item['sort'];
  }
  //记录要拖动的id
  $oldValue = $idArr[$oldIndex];
  //删除这个要拖动的id
  unset($idArr[$oldIndex]);
  //插入新的位置,并自动移位
  array_splice($idArr, $newIndex, 0, $oldValue);
  //重新设置排序
  $set = [];
  for ($i = 0; $i < count($idArr); $i++) {
     $set[$i][&#39;id&#39;]  = $idArr[$i];
     $set[$i][&#39;sort&#39;] = $sortArr[$i];
   }
  //保存到数据库省略
}
Copy after login

Related recommendations:

php interface programming detailed explanation


The above is the detailed content of PHP interface implements drag and drop sorting function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template