Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6618111
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:47:14+00:00 2026-05-25T20:47:14+00:00

I have tree where user can drag and drop element and change elements positions.

  • 0

I have tree where user can drag and drop element and change elements positions.
After every change I’m sending this serialize array and I want to save it .

Example tree :

Economics
Blogging
General Dev
Japan
Productivity

    Humanities
        Education
        Science
            Haskell
                Earth
            PHP

I’m sending serialize tree throught ajax so I have array linke this.

'0' => "1"
'1' => "2"
'2' => "3"
'3' => "4"
'4' ...
    '0' => "5"
    '1' ...
        '0' ...
            '0' => "6"
            '1' ...
                '0' => "7"
                '1' ...
                    '0' => "8"
                    '1' ...
                        '0' ...
                            '0' => "9"
                            '1' ...
                                '0' => "10"
                        '1' ...
                            '0' => "11"

Array value is table row id.

how insert this array into database with correct position ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:47:15+00:00Added an answer on May 25, 2026 at 8:47 pm

    I would suggest using traditional tree storing approach:

    create table catalogue (
        id int not null auto_increment primary key,
        name varchar(255),
        catalogue_id int,
        ord int
    );
    

    So first of you need to fill up db properly, then rest should be easy to handle.

    actual code.

    Client side:

       <ul id="enclosure_0" class="sortable">
            <li id="item_1">Level0 - Item 1</li>
            <li id="item_2">Level0 - Item 2</li>
            <li id="item_3">Level0 - Item 3</li>
            <li id="item_4">Level0 - Item 4</li>
            <li id="item_5">Level0 - Item 5
                <ul id="enclosure_5" class="sortable">
                    <li id="item_6">Level1 - Item 1</li>
                    <li id="item_7">Level1 - Item 2</li>
                    <li id="item_8">Level1 - Item 3</li>
                    <li id="item_9">Level1 - Item 4</li>
                </ul>
            </li>
       </ul>        
       <script>     
           $(document).ready(function(){
               $(".sortable").sortable({
                   stop: function(ev, ui){
                       var list = $(this).sortable("toArray");
                       $.get("sort.php", {items: list, parent_id: $(this).attr('id')}, function(data){window.alert("stored");}); 
                    }
                });
            });
        </script>
    

    Server side (sort.php):

    <?php
    
    /* you have new order in $_GET["items"], you have parent id in $_GET["parent_id"] */
    
    /* easiest way:
     * */
    
    preg_match("/enclosure_([0-9])+/", $_GET["parent_id"], $tmp);
    $parent_id = (int) $tmp[1];
    $counter = 0;
    foreach ($_GET["items"] as $item){
        $counter++;
        preg_match("/item_([0-9]+)/", $item, $tmp);
        $item_id = (int) $tmp[1];
        $db->query("update catalogue set ord = $counter where id = $item_id");
        /* alternatively, somewhat safer */
        $db->query("update catalogue set ord = $counter where id = $item_id and catalogue_id = $parent_id");
    } 
    

    Notes and assumptions:

    1) catalogue_id should be 0 for top level categories
    2) I believe you are smart enough to get the client side dynamically rendered.
    3) Assumes ord is 1 based.
    4) php not tested, but should be ok

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JTree in which the user can drag/drop or re-arrange nodes, upon
I am working on the flex tree drag and drop functionality and i have
I have a tree control and I want to give the user the ability
Ok I am looking for a solution in mvc2 where i can have tree
I am doing a drag drop from one user control onto a different TreeView.
I have a treeview in which a user can move, delete subtrees, nodes and
I have a Jtree with nodes representing images that the user can call up
I have a tree with nodes, and a delete button, first user select the
I am denying a user the ability to drop into my tree during certain
I have made an application with a Tree and a TreeView where the user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.