Image Drag & Drop Shuffle & Reorder Position

This tutorial we will create a Image Drag & Drop Shuffle & Reorder Position. And would be very helpful for your programming projects. This project you will learn how to build drag and drop shuffle & reorder position of images using jQuery, Ajax, PHP and MySQL. And you would be able to provide a better user interface for your web project. You can use this functionality for managing images gallery, managing users list or any other useful place.

Sample Code

Script for the function of drag & drop of images in the gallery.
  1. <script type="text/javascript">
  2. $(document).ready(function(){
  3.         $('.reorder_link').on('click',function(){
  4.                 $("ul.reorder-photos-list").sortable({ tolerance: 'pointer' });
  5.                 $('#reorder-details').slideDown('slow');
  6.                 $('.reorder_link').html('save shuffled image');
  7.                 $('.reorder_link').attr("id","save");
  8.                 $('.image_link').attr("href","javascript:void(0);");
  9.                 $('.image_link').css("cursor","move");
  10.                 $("#save").click(function( e ){
  11.                         if( !$("#save i").length )
  12.                         {
  13.                                 $(this).html('').prepend('<img src="images/loading.gif"/>');
  14.                                 $("ul.reorder-photos-list").sortable('destroy');
  15.                                 var h = [];
  16.                                 $("ul.reorder-photos-list li").each(function() {  h.push($(this).attr('id').substr(9));  });
  17.                                 $.ajax({
  18.                                         type: "POST",
  19.                                         url: "update.php",
  20.                                         data: {ids: " " + h + ""},
  21.                                         success: function(html)
  22.                                         {
  23.                                                 window.location.reload();
  24.                                         }
  25.                                 });    
  26.                                 return false;
  27.                         }      
  28.                         e.preventDefault();            
  29.                 });
  30.         });
  31. });
  32. </script>
And for the image shuffle/reorder. The file receive the current images order from the index.php through Ajax and update the images reorder at the database.
  1. <?php
  2. include_once('db.php');
  3.         $db = new database();
  4.         $idArray = explode(",",$_POST['ids']);
  5.         $db->updateOrder($idArray);
  6. ?>
result Hope that you learn in this project and enjoy coding. Don't forget to LIKE & SHARE this website.

Add new comment