This time for everyone to bring PHP interface to implement the drag-and-drop sequencing steps, PHP interface to achieve the attention of the drag-and-drop sorting, the following is the actual case, take a look.
How can we achieve the highest efficiency?
First analyze a scene, if there is a page has 10 data, so-called drag is in the 10 data to the back and forth of the drag, but each drag will affect the other data such as drag the last one to the front, then nine automatically backward, and vice versa, hmm ~ ~ ~
First imagine that the row ordinal is fixed, as if there are 10 chairs, each seat is fixed there, moving is the person above, so that it will not affect the data of other pages and everyone is also the other person's table and chairs number, so also do not have to think how much to add to the row where.
Interface design:
$ids the ID collection of the 10 data, comma-separated string//$oldIndex original position, counting from 0//$newIndex position to drag function dragsort ($ids, $oldIndex, $newIndex) { //Guaranteed to find the data in the same order as the front table commits, here to order by field //id primary key sort value $sql = "Select Id,sort from table name where ID in ($ids) o Rder by field (ID, ". $ids. ") "; $list = "Omitted here, is to go to the database to find"; ID Collection $IDARR = []; Sort set $SORTARR = []; foreach ($list as $item) { $IDARR [] = $item [' id ']; $SORTARR [] = $item [' sort ']; } Record the ID you want to drag $oldValue = $idArr [$oldIndex]; Delete the ID unset to be dragged ($IDARR [$oldIndex]); Insert a new position and automatically shift the Array_splice ($IDARR, $newIndex, 0, $oldValue); Reset sort $set = []; for ($i = 0; $i < count ($IDARR), $i + +) { $set [$i] [' id '] = $IDARR [$i]; $set [$i] [' sort '] = $SORTARR [$i]; } Save to Database omitted}
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
PHP7 Operation MongoDB Additions and deletions to investigate the steps
A detailed description of the PHP operation Redis Step