Detailed explanation of the steps to implement drag-and-drop sorting using PHP interface

php中世界最好的语言
Release: 2023-03-25 22:56:02
Original
1882 people have browsed it

This time I will bring you a detailed explanation of the steps to implement drag-and-drop sorting through the PHP interface. What are the precautions for implementing drag-and-drop sorting through the PHP interface? The following is a practical case, let’s take a look.

How to achieve the highest efficiency?

First analyze a scenario. If there is a page with ten pieces of data, the so-called drag and drop is Drag these ten pieces of data back and forth, but each drag will affect other data. For example, if you drag the last piece to the front, the next nine pieces will automatically move to the back, and vice versa, eh~~~

Imagine first that the sort number is fixed, just like there are ten chairs, each chair is fixed there, and the people on it are moved, so that it will not affect the data of other pages and each person changes The numbers are also the table and chair numbers of other people before, so you don’t have to think about how many more you need to add to get where you are ranked.

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]['id']  = $idArr[$i];
     $set[$i]['sort'] = $sortArr[$i];
   }
  //保存到数据库省略
}
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed explanation of the steps for adding, deleting, modifying and querying in PHP7

Detailed explanation of the steps for PHP operating Redis

The above is the detailed content of Detailed explanation of the steps to implement drag-and-drop sorting using PHP interface. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!