snarore Drag and Drop Reorder Images using Query, Ajax, PHP & MySQL - CodexWorld
Home (https://www.codexworld.com) » PHP (https://www.codexworld.com/tutorials/php/) » Drag
and Drop Reorder Images using jQuery, Ajax, PHP & MySQL
Drag and Drop Reorder Images using jQuery,
Ajax, PHP & MySQL
By: CodexWorld | In: PHP (https://www.codexworld.com/tutorials/php/) | Last Updated: Mar 8, 2018
Drag and Drop Reorder Images using jQuery, Ajax, PHP & MySQL
Drag and Drop feature makes web page user-friendly and provides a nice user interface for
the web application. jQuery UI provides an easy way to add draggable functionality on DOM
element. This tutorial shows you the uses of jQuery drag and drop feature to sort the list
elements.
If we want to control the display order of images in the list dynamically, image order needs
to be stored in the database. In this tutorial, we'll provide a more interactive way to
implement the images reorder functionality, Here we'll demonstrate how to add jQuery
drag and drop feature to rearrange images order and save images display order in the
database using PHP and MySQL. The Drag and Drop Reorder functionality are very useful
for managing images gallery, users list, and other dynamic content lists.
The example code helps to implement dynamic drag and drop reorder images using
jQuery, Ajax, PHP, and MySQL. This sample code also can be used to implement the drag
and drop reorder list, rows or sorting elements.
hitps:luwn-codexworld.comidrag-drop-mages-reorder-sing-jquery-slax-ahp-mysqU wasiroo18 Drag and Drop Reorder Images using {Query Ajax, PHP & MySQL. - Codexold
Before getting started, take a look at the folder and files structure to build drag and drop
reorder images functionality using jQuery, Ajax, PHP, and MySQL.
DB.class.php
orderUpdate.php
index. php
style.css
images/
Create Database Table
To store images and their display order, a table needs to be created in the database. The
following SQL creates a images table with some basic required fields in the MySQL
database.
CREATE TABLE “images” (
“id? int(24) NOT NULL AUTO_INCREMENT,
file_name” varchar(1@0) COLLATE utf8_unicode_ci NOT NULL,
img_order” int(S) NOT NULL DEFAULT ‘0",
created’ datetime NOT NULL,
“modified” datetime NOT NULL,
status’ enum('1',*@") COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
PRIMARY KEY (“id”)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
DB Class (DB.class.php)
The DB class controls all the database related works, like get records, update record, etc.
Specify the database host ( $dbHost ), username ( $dbUsernane ), password ( $dbPassword ), and
name ( $dbNane ) as per your MySQL database credentials.
The DB class contains two methods to fetch and update images data
* getRows() function fetch the images data from the database.
* updateorder() function updates list order of the images.
hitps:luw-codexworld.comidrag-drop-mages-reorder-sing-jquery-asnarore Drag and Drop Reorder Images using Query, Ajax, PHP & MySQL - CodexWorld
db) ){
// Connect to the database
$conn = new mysqli($this-sdbHost, $this->dbUsername, $this->doPassword, $t
his->dbName) ;
if ($conn->connect_error){
die(“Failed to connect with MySQL:
Jelset
Sthis->db = $conn;
+ $conn->connect_error);
}
+
+
I
* Get rows from images table
*/
function getRows(){
Squery = $this->db->query("SELECT * FROM “.$this->imgTbl." ORDER BY img _orde
PASC");
if(Squery->num_rows > @){
while($row = $query->fetch_assoc()){
Sresult[] = $row;
+
jelse{
$result = FALSE;
?
return $result;
I
* Update image order
*/
function updateOrder($id_array){
$count = 1;
foreach ($id_array as $id){
$update = $this->db->query("UPDATE ".$this->imgTbl." SET img_order = $coun
t WHERE id = $id");
$count ++;
+
return TRUE;
hitps:luwu.codexworld.comidrag-drop-mages-reorderusing-jquery
2-shp-mysqlsnarote Drag and Drop Reorder Images using Query, Ajax, PHP & MySQL - CodexWorld
}
2
Reorder Images with Drag and Drop (index.php)
The index.php file display the images and allow the user to reorder images by drag and
drop.
jQuery & Ajax Code:
At first, include the jQuery and jQuery UI library to use Ajax and Draggable functionality
The following jQuery code is used to enable the jQuery U! Draggable & Sortable features
and implement the drag & drop functionality. When order save request is submitted,
current images order send to the orderUpdate.php file via Ajax to update the images order
in the database.
hitps:luw-codexworld.comidrag-drop-mages-reorder-sing-jquery-asnarore Drag and Drop Reorder Images using Query, Ajax, PHP & MySQL - CodexWorld
PHP & HTML Code:
Initially, all the images are retrieved from the database and listed as per the order specified
in ing_order field of images table using PHP and DB class. Once the reorder link is clicked,
drag & drop feature is enabled to reorder the images.
hitps:luwu.codexworld.comidrag-drop-mages-eorder-using-jquery
2-shp-mysqlsnarore Drag and Drop Reorder Images using Query, Ajax, PHP & MySQL - CodexWorld
1. Drag photos to
reorder. 2. Click ‘Save Reordering’ when finished.