• We just launched and are currently in beta. Join us as we build and grow the community.

Creating a Dynamic Ordering Data using PHP, MySQL, and JavaScript Tutorial

rockshock

Digital Ethics Advocate
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
106
Likes
24
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, you can learn to create Dynamic Ordering Data and DOM Elements using PHP, JavaScript, and MySQL Database. This tutorial aims to provide students and beginners with a reference for learning some useful techniques for building an application using PHP Language, MySQL, and JavaScript. Here, I will be providing a simple web application that demonstrates the main objective of this tutorial. The complete source code zip file will be provided also and is free to download.

What is Dynamic Ordering Data?

Dynamic Ordering Data is a simple web application database table structure that allows the user to choose or set their desired order of data. Basically, we can simply sort the data order by each column on the database but in some instances, we will be forced to implement this kind of structure and add some interactive feature to front-end of a software or application to meet our requirements. For example, an application has a dynamic navigation menu data whereas the developer saves the menu data on the database. Using this kind of database structure, we can sort the order of the menu items for display dynamically.

How to create Dynamic Ordering Data and DOM Elements?

Dynamic Ordering Data and DOM Elements can be achieved using PHP, JavaScript, and MySQL Database. On the database table that we want to dynamically sort the order, we can simply add a column with an integer type of value. The added table column will store the order number of each data. Next, using CSS and JavaScript, we can create a list item element that allows the users to drag and drop the element from the current position to another. Then, using PHP, we can update the new sort order of the data on the database. Check out the sample web application scripts that I created and provided below to understand it more.

Sample Web Application

The scripts below result in a simple web application that contains a simple list of items in which each item is being retrieved from the database. Users can change the order position of the data by grabbing the data element from the current position and dropping it before or after the list item. Then, by hitting or clicking the "Save Order" button, the new sort order of the data will be updated on the database.

Before we start the coding part, please make sure to have virtual server software such as XAMPP, WAMP, or any similar software to allow your local machine to run PHP scripts. Also, make sure to run Apache and MySQL.

Creating the Database

First, on your MySQL Database, create a new database named dummy_db. Copy and Paste the DB Schema and sample data below.

  1. -- phpMyAdmin SQL Dump
  2. -- version 5.1.3
  3. -- https://www.phpmyadmin.net/
  4. --
  5. -- Host: 127.0.0.1
  6. -- Generation Time: Apr 28, 2023 at 09:34 AM
  7. -- Server version: 10.4.24-MariaDB
  8. -- PHP Version: 8.1.5

  9. SET

    SQL_MODE =

    "NO_

    AUTO_

    VALUE_

    ON_

    ZERO"

    ;
  10. START

    TRANSACTION

    ;
  11. SET

    time_zone =

    "+00:00"

    ;

  12. --
  13. -- Database: `dummy_db`
  14. --

  15. -- --------------------------------------------------------

  16. --
  17. -- Table structure for table `sampletbl`
  18. --

  19. CREATE

    TABLE

    `sampletbl`

    (
  20. `id`

    int

    (

    1

    )

    NOT

    NULL

    ,
  21. `content`

    text

    NOT

    NULL

    ,
  22. `sort_

    order`

    tinyint

    (

    3

    )

    NOT

    NULL


  23. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

  24. --
  25. -- Dumping data for table `sampletbl`
  26. --

  27. INSERT

    INTO

    `sampletbl`

    (

    `id`

    ,

    `content`

    ,

    `sort_

    order`

    )

    VALUES


  28. (

    1

    ,

    'Sample Data Only #1'

    ,

    0

    )

    ,
  29. (

    2

    ,

    'Sample Data Only #2'

    ,

    1

    )

    ,
  30. (

    3

    ,

    'Sample Data Only #3'

    ,

    2

    )

    ,
  31. (

    4

    ,

    'Sample Data Only #4'

    ,

    3

    )

    ,
  32. (

    5

    ,

    'Sample Data Only #5'

    ,

    4

    )

    ;

  33. --
  34. -- Indexes for dumped tables
  35. --

  36. --
  37. -- Indexes for table `sampletbl`
  38. --
  39. ALTER

    TABLE

    `sampletbl`
  40. ADD

    PRIMARY KEY

    (

    `id`

    )

    ;

  41. --
  42. -- AUTO_INCREMENT for dumped tables
  43. --

  44. --
  45. -- AUTO_INCREMENT for table `sampletbl`
  46. --
  47. ALTER

    TABLE

    `sampletbl`
  48. MODIFY

    `id`

    int

    (

    1

    )

    NOT

    NULL

    AUTO_INCREMENT

    ,

    AUTO_INCREMENT

    =

    6

    ;
  49. COMMIT

    ;


Creating the Database Connection

Next, create a new PHP file and save it as db-connect.php. Then paste the following script to this file script. This script connects our web application to the MySQL Database that we created.

php $host = "localhost";
$username = "root";
$password = "";
$dbname = "dummy_db";

try{
$conn= new MySQLi($host, $username, $password, $dbname);
}catch (Exception $e){
throw new ErrorException($e-getMessage());
exit;
}

Creating the Page Interface

Next, let's create the page interface of the web application. On your source code directory, create a new PHP file named index.php and copy/paste the code below.

  1. <?php
  2. require_once(

    'db-connect.php'

    )
  3. ?>
  4. <!DOCTYPE html>
  5. <html

    lang

    =

    "en"

    >
  6. <head

    >
  7. <meta

    charset

    =

    "UTF-8"

    >
  8. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  9. <title

    >

    Dynamic Ordering</

    title

    >
  10. <link

    rel

    =

    "stylesheet"

    href

    =

    "style.css"

    >
  11. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.googleapis.com"

    >
  12. <link

    rel

    =

    "preconnect"

    href

    =

    "https://fonts.gstatic.com"

    crossorigin>
  13. <link

    rel

    =

    "stylesheet"

    href

    =

    "https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200"

    /

    >
  14. </

    head

    >
  15. <body

    >
  16. <div

    class

    =

    "container"

    >
  17. <h1

    id

    =

    "page-title"

    >

    Creating a Dynamic Ordering list Item JS, PHP, and MySQL</

    h1

    >
  18. <hr

    id

    =

    "title_hr"

    >
  19. <form

    action

    =

    "update_order.php"

    method

    =

    "POST"

    id

    =

    "dynamic-order"

    >
  20. <!-- Sortable list Container -->
  21. <div

    class

    =

    "sortable-list-container"

    >
  22. <!-- List Wrapper -->
  23. <ul

    >
  24. <?php
  25. $list_qry =

    $conn->

    query("SELECT * FROM `sampletbl` order by `sort_order` asc ");
  26. while($row = $list_qry->fetch_assoc()):
  27. ?>
  28. <!-- List item -->
  29. <li

    draggable

    =

    "true"

    >
  30. <div

    class

    =

    "flex-container"

    >
  31. <div

    >
  32. <span

    class

    =

    "material-symbols-outlined"

    >
  33. drag_indicator
  34. </

    span

    >
  35. </

    div

    >
  36. <div

    >
  37. <input

    type

    =

    "hidden"

    name

    =

    "sort_order[]"

    value

    =

    "<?= $row['id'] ?>

    ">
  38. <?=

    $row[

    'content'

    ]

    ?>
  39. </

    div

    >
  40. </

    div

    >
  41. </

    li

    >
  42. <!-- List item -->
  43. <?php endwhile; ?>
  44. </

    ul

    >
  45. <!-- List Wrapper -->
  46. </

    div

    >
  47. <!-- Sortable list Container -->
  48. </

    form

    >
  49. <!-- Form Submit Button -->
  50. <button

    id

    =

    "save-btn"

    type

    =

    "submit"

    form

    =

    "dynamic-order"

    >

    Save Order</

    button

    >
  51. </

    div

    >

  52. <script

    src

    =

    "script.js"

    ></

    script

    >
  53. </

    body

    >
  54. </

    html

    >

Then, create a new file named style.css and use the following script. The script below contains the CSS (stylesheet) code that designs the page layout and other elements of the page.

  1. @import

    url

    (

    'https://fonts.googleapis.com/css2?f...00;1,200;1,300;1,400;1,500;1,600&display=swap" rel="stylesheet'

    )

    ;
  2. *

    {
  3. margin

    :

    0

    ;
  4. padding

    :

    0

    ;
  5. box-sizing

    :

    border-box

    ;
  6. font-family

    :

    'Dongle'

    ,

    sans-serif

    ;
  7. font-family

    :

    'Roboto Mono'

    ,

    monospace

    ;
  8. }
  9. ::

    selection

    {
  10. color

    :

    #fff

    ;
  11. background

    :

    #4db2ec

    ;
  12. }
  13. body{
  14. display

    :

    flex;
  15. align-items

    :

    center

    ;
  16. justify-content

    :

    center

    ;
  17. min-height

    :

    100vh

    ;
  18. background

    :

    #4facfe

    ;
  19. background-image

    :

    linear-gradient(

    to right

    ,

    #4facfe

    0%

    ,

    #00f2fe

    100%

    )

    ;
  20. padding

    :

    2em

    0

    ;
  21. }
  22. #page-title

    {
  23. color

    :

    #fff

    ;
  24. text-align

    :

    center

    ;
  25. font-weight

    :

    500

    ;
  26. text-shadow

    :

    0px

    0px

    15px

    #0000003a

    ;
  27. }
  28. #title_hr

    {
  29. width

    :

    60px

    ;
  30. border

    :

    2px

    solid

    #ffffff

    ;
  31. margin

    :

    .35em

    auto

    ;
  32. }
  33. @media

    (

    min-width

    :

    780px

    )

    {
  34. #page-title

    {
  35. width

    :

    780px

    ;
  36. }
  37. }


  38. /* Sortable List */
  39. .sortable-list-container{
  40. width

    :

    400px

    ;
  41. background-color

    :

    #fff

    ;
  42. border

    :

    1px

    solid

    #dedede

    ;
  43. box-shadow

    :

    1px

    1px

    5px

    #6c6c6c

    ;
  44. margin

    :

    1em

    auto

    ;
  45. }
  46. .sortable-list-container

    ul{
  47. padding

    :

    .5em

    1em

    ;
  48. }
  49. .sortable-list-container

    ul li{
  50. list-style

    :

    none

    ;
  51. border

    :

    1px

    solid

    #cfcfcf

    ;
  52. box-shadow

    :

    1px

    1px

    5px

    #a8a8a8

    ;
  53. margin-bottom

    :

    10px

    ;
  54. padding

    :

    0.5em

    1em

    ;
  55. cursor

    :

    grab;
  56. background

    :

    #fff

    ;
  57. }
  58. .sortable-list-container

    ul li[

    data-dragging=

    "true"

    ]

    {
  59. border-style

    :

    dashed

    ;
  60. box-shadow

    :

    unset;
  61. }
  62. .sortable-list-container

    ul li[

    data-dragging=

    "true"

    ]

    :

    where(

    *

    )

    {
  63. opacity

    :

    0

    ;
  64. }

  65. /* Flex Container */
  66. .flex-container{
  67. display

    :

    flex;
  68. width

    :

    100%

    ;
  69. flex-wrap

    :

    wrap;
  70. align-items

    :

    center

    ;
  71. }

  72. /* Save Button */
  73. button

    #save-btn

    {
  74. display

    :

    block

    ;
  75. width

    :

    150px

    ;
  76. background

    :

    #438efe

    ;
  77. padding

    :

    0.5em

    1em

    ;
  78. color

    :

    #fff

    ;
  79. font-size

    :

    1rem

    ;
  80. font-weight

    :

    500

    ;
  81. margin

    :

    0.35em

    auto

    ;
  82. border

    :

    none

    ;
  83. box-shadow

    :

    0px

    0px

    10px

    #00000030

    ;
  84. cursor

    :

    pointer

    ;
  85. }
  86. button

    #save-btn

    :

    hover

    {
  87. background

    :

    #2578f5

    ;
  88. }


Creating the Update API

Lastly, let's create a new PHP file named update_order.php and use the following script. This file script will execute the update process of the new sort order of data into the database table.

  1. <?php
  2. // Require or include once the db connection handler script
  3. require_once

    (

    'db-connect.php'

    )

    ;

  4. if

    (

    $_SERVER

    [

    'REQUEST_METHOD'

    ]

    ==

    'POST'

    )

    {
  5. // If Post data exists
  6. if

    (

    isset

    (

    $_POST

    [

    'sort_order'

    ]

    )

    )

    {
  7. // If Sort Order Sample Post data Exists
  8. // set post data into a variable
  9. $data_ids

    =

    $_POST

    [

    'sort_order'

    ]

    ;

  10. // loop the data
  11. foreach

    (

    $data_ids

    as

    $order

    =>

    $id

    )

    {
  12. // update the sort order of the data
  13. $update

    =

    $conn

    ->

    query

    (

    "UPDATE `sampletbl` set `sort_order` = '{$order}

    ' where `id` ='{$id}

    '"

    )

    ;
  14. if

    (

    !

    $update

    )

    {
  15. // If query execution fails, return an error message
  16. echo

    "<script>alert('An error occured while updating data!'); location.replace('./')</script>"

    ;
  17. }
  18. }
  19. echo

    "<script>alert('Data Sort Order has been updated successfully!'); location.replace('./')</script>"

    ;
  20. }

    else

    {
  21. // No Sort Order Post Data sent
  22. echo

    "<script>alert('No Sort Data sent!'); location.replace('./')</script>"

    ;
  23. }
  24. }

    else

    {
  25. // If no post data send to this request
  26. echo

    "<script>alert('No Post Data sent!'); location.replace('./')</script>"

    ;
  27. }

Snapshots

Here are some snapshots of the overall result of the web application script I have provided above:

Default

Sample Updated Sort Order #1

Sample Updated Sort Order #2

DEMO VIDEO

There you go! I have provided the complete source code zip file of the sample web application script I created on this site and it is free to download. The download button can be found below this tutorial's content. Feel free to download and do some experiments with the source code to enhance your programming capabilities.

That's it! I hope this Dynamic Ordering Data and DOM Element using PHP, MySQL, and JavaScript Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

?>


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top