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]; } //保存到数据库省略 }
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!