phpjqueryajaxmeekro

Changing a PHP/AJAX script to work with MeekroDB?


I'm trying to "convert" a very small project (list of data that can be re-arranged by dragging and dropping) from, well, I suppose usual PHP, mysqli and ajax to working with MeekroDB instead.

It works fine in its original form (this is the small PHP file that the Ajax uses):

<?
 require_once __DIR__.'/../ini.php'; 

$SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];

if(count($SortingOrder)>0){
    for($order_no= 0; $order_no < count($SortingOrder); $order_no++)
    {
     $query = "UPDATE SortingTable SET SortingOrder = '".($order_no+1)."' WHERE ID = '".$SortingOrder[$order_no]."'";
     mysqli_query($con, $query);
    }
    echo true; 
}else{
    echo false; 
}

?>

But does absolutely nothing now that I'm trying with MeekroDB:

<?
 require_once __DIR__.'/../ini_meekro.php'; 

$SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];


if(count($SortingOrder)>0){
  for($order_no= 0; $order_no < count($SortingOrder); $order_no++) 
  {
    
    DB::query("UPDATE SortingTable SET SortingOrder=%i, WHERE ID=%i", $order_no+1, $SortingOrder[$order_no]);
  };

    echo true; 
}else{
    echo false; 
}

?>

The javascript/jQuery script is unchanged. Of course, I have updated how the list is initially fetched from the database and displaying on the site, and that works just fine. I can also still drag things, but it never saves the new sorting order.

I have never done anything with Ajax before. I initially used the guide found here: http://learninfinity.info/drag-and-drop-table-row-sorting-ajax-php-and-mysqli/ and got it all to work nicely. But I would like it to work with MeekroDB instead, and I'm stuck.


Solution

  • After way too long I finally found a solution.

    I don't really know or understand the big difference in the two ways to update in MeekroDB, but using the other way and making variables works.

    <?
     require_once __DIR__.'/../ini_meekro.php'; 
    
    $SortingOrder = isset($_POST["SortingOrder_ids"]) ? $_POST["SortingOrder_ids"] : [];
    
    
    if(count($SortingOrder)>0){
      for($order_no= 0; $order_no < count($SortingOrder); $order_no++) 
      {
        
        $NewOrderNumber = $order_no+1;
        $EntryID = $SortingOrder[$order_no];
        
        //DB::query("UPDATE SortingTable SET SortingOrder=%i, WHERE ID=%i", $order_no+1, $SortingOrder[$order_no]);
        DB::update('SortingTable', ['SortingOrder' => $NewOrderNumber], "ID=%i", $EntryID);
      };
    
        echo true; 
    }else{
        echo false; 
    }
    
    ?>
    

    I'll leave this up here, but I'll be very happy if someone knows a better and cleaner solution! :)