I'm using JQuery to provide my users with a re-arrangeable list of items. Like this:
Here's the simple HTML:
<ul class="sortable">
<li>Item 1<i class="sorthandle"></i></li>
<li>Item 2<i class="sorthandle"></i></li>
</ul>
The drag-and-drop feature works great. The next step is saving the user's sort preference to the database. I plan to add a "priority" column to my database, which will hold an integer representing the user's preferred sort order. My query will then Order by 'priority' ASC
.
I'm thinking I need to generate an array of the sort order currently displayed on the drag-and-drop screen, then save those values to the database records. But as a JavaScript newbie, I need some help.
My JavaScript
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function() {
$(".sortable").sortable({
connectWith: ".sortable",
handle: '.handle'
}).disableSelection();
});
</script>
Generating the drag-and-drop list via PHP:
while ($row = mysqli_fetch_array($result)){
$playlistname = $row['name'];
$id = $row['id'];
echo "<li>$playlistname<i class='handle fa fa-bars'></i></li>";
}