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

  • SEARCH
  • Home
  • 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 8879413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:56:43+00:00 2026-06-14T19:56:43+00:00

So I have a bit of a problem. I have created a drag and

  • 0

So I have a bit of a problem. I have created a drag and drop list within my WordPress plugin that is supposed to allow the user to order the categories, for the most Part I have this working…

The table is populated from a MySQL database:

$results = $wpdb->get_results($sql);

    foreach($results as $result)
    {
    ?>
     <tr id="list_item_<?php echo $result->priority_order; ?>" class="list_item">
                <td>
                    <?php echo $result->name; ?>
                </td>
                <td>
                    <?php echo $result->priority_order; ?>
                </td>
            </tr>
    <?php
    }

and I am using ‘jquery-ui-sortable’ in order to allow the table to be sortable.

here is my JQuery for making the list drag-able and to update the list:

$('.list').sortable(
{
    items: '.list_item',
    opacity: 0.6,
    cursor: 'move',
    axis: 'y',
    update: function()
    {
        var order = $(this).sortable('serialize') + '&action=update_order';
        $.post(ajaxurl, order, function(response)
        {
            //alert(response);
        });
    }
});

here is the PHP used to update the database with the new order:

function save_order()
{
  global $wpdb;

  $table_name = $wpdb->prefix . "categories";
  $sql = "SELECT * FROM ".$table_name." ORDER BY priority_order ASC";

  $results = $wpdb->get_results($sql);
  $results = parseObjectToArray($results);

  $new_order = $_POST["list_item"];

  $counter = 0;
  foreach($new_order as $v)
  {
    if(isset($results[$v]["priority_order"]))
    {
        $update_sql = "UPDATE ".$table_name." SET priority_order='".$counter."' WHERE category_id='".$results[$v]["category_id"]."'";
        $wpdb->query($update_sql);

        $counter++;
    }
  }
  die();
}
add_action('wp_ajax_update_order','save_order');

// Converts StdObject to regular array
function parseObjectToArray($object)
{
$array = array();

  foreach($object as $o)
  {
    if (is_object($o))
    {
      $array[] = get_object_vars($o);
    }
  }
  return $array;
}

My problem is that while the list updates perfectly the firs time, the next serialized array that is sent assuming the page has not been manually refreshed is now updating incorrectly. This is because the new list and the newly updated one from the database are no longer synchronized.

This is because I am using the array index to match up what needs updating, but after the initial change, the new list index and the index coming from the array no loner match…

Does anyone have any idea’s on how I can solve this problem? I have looked around quite a bit and most of the code Ive found is similar to mine and I cant see any noticeable difference that might cause mine to break. Will I need to refresh the page after each update?

  • 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-06-14T19:56:44+00:00Added an answer on June 14, 2026 at 7:56 pm

    I found the answer myself yet again, haha…

    I was concentrating to much on the pirority_order field, instead I sent each category ID and used SQL to ORDER BY priority_id, then simply with a counter I could assign a new order.

    global $wpdb;
    
    $table_name = $wpdb->prefix . "categories";
    $sql = "SELECT * FROM ".$table_name." ORDER BY priority_order ASC";
    
    $results = $wpdb->get_results($sql);
    $results = parseObjectToArray($results);
    
    $new_order = $_POST["ebs_list_item"];
    
    $counter = 0;
    foreach($new_order as $v)
    {
        $update_sql = "UPDATE ".$table_name." SET priority_order=".$counter." WHERE id=".$v;
        $wpdb->query($update_sql);
    
        $counter++;
    }
    die();
    

    <tr id="list_item_<?php echo $result->id; ?>" class="list_item">
        <td>
            <?php echo $result->name; ?>
        </td>
        <td>
            <?php echo $result->priority_order; ?>
        </td>
    </tr>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of a problem that I can't seem to code my
I have a bit of a strange problem. I have a music app that
I'm having a bit of a jQuery problem. Basically I have some functions that
I have the Problem that I created a Service which should be created when
I'm having a bit of a problem I have created a post and it
i have a problem with the order of execution of the threads created consecutively.
I have a bit of problem when trying to validate my page as HTML5.
I have a bit of a problem. I am trying to do the following
I have a bit of a problem building my project. I'm getting the bellow
I have a bit of a problem here. I have a third party ActiveX

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.