Problem saving image order in Database

0

I have a problem keeping the order of my images in the database, first I have a JS file where I store the new order in a variable, when I open a console.log, if it shows it correctly, then I send that data to an AJAX file that sends it to the controller and then to the model, but the latter does not keep the order in the DB, and does not show any error, I put the code, waiting for someone to see something that I do not.

NOTE * I only put the lines of code corresponding to the order, omitting unnecessary classes and things, however, if you need more information, I will gladly put it, I have also omitted routes and table names.

Thank you very much (=

JS

for(var i = 0; i < $('#desul li').length; i++)
{
   almacenarOrdenId[i] = event.target.children[i].id;
   ordenItem[i] = i+1;
}

$('#btnDesignPortSave').click(function(){

for(var i = 0; i < $('#desul li').length; i++)
{
    var actualizarOrden = new FormData();
    actualizarOrden.append("actualizarOrdenGaleria", almacenarOrdenId[i]);
    actualizarOrden.append("actualizarOrdenItem", ordenItem[i]);

    $.ajax({

        url:"PATH TO AJAX FILE",
        method: "POST",
        data: actualizarOrden,
        cache: false,
        contentType: false,
        processData: false,
        success: function(respuesta)
        {
            $('#desport ul').html(respuesta);

            swal("Order updated", {
                buttons: false,
                timer: 2000,
            }).then(
                function(){
                    window.location = "designPortfolio";
                }
            );
          }
      })//ajax
   }//for

})//Btn

AJAX FILE

public $actualizarOrdenGaleria;
public $actualizarOrdenItem;

public function actualizarDordenAjax()
{

    $datos = array("ordenGaleria" => $this -> actualizarOrdenGaleria,
                    "ordenItem" => $this -> actualizarOrdenItem);

    $respuesta = gestorDgaleriaController::actualizarOrdenController($datos);

    echo $respuesta;

}

///////////////////////////////////////////////

if(isset($POST["actualizarOrdenGaleria"]))
{
   $c = new Ajax();
   $c -> actualizarOrdenGaleria = $_POST["actualizarOrdenGaleria"];
   $c -> actualizarOrdenItem = $_POST["actualizarOrdenItem"];
   $c -> actualizarDordenAjax();

}

PHP

////CONTROLLER

public function actualizarOrdenController($datos)
{
    gestorDgaleriaModel::actualizarOrdenModel($datos,"DB TABLE");
    $respuesta = gestorDgaleriaModel::seleccionarOrdenModel("DB TABLE");

    foreach($respuesta as $row => $item)
    {
        echo'<li id="'.$item["id"].'" class="bloqueDGaleria">
                <i class="fa fa-times delbtn portdesdel" aria-hidden="true" ruta="'.$item["ruta"].'"></i>
                <a data-fancybox="portdes" href="'.substr($item["ruta"],6).'">
                    <img src="'.substr($item["ruta"],6).'" class="handleDImg"/>
                </a>
            </li>';


    }

}

////MODEL

public function actualizarOrdenModel($datos,$tabla)
{
    $stmt = conexion::conectar()->prepare("UPDATE $tabla SET orden = :orden WHERE id = :id");
    $stmt -> bindParam(":orden", $datos["ordenItem"], PDO::PARAM_INT);
    $stmt -> bindParam(":id", $datos["ordenGaleria"], PDO::PARAM_INT);

    if($stmt->execute())
    {
        return "ok";
    }
    else
    {
        return "error";
    }

    $stmt -> close();


}


public function seleccionarOrdenModel($tabla)
{
    $stmt = conexion::conectar()-> prepare("SELECT id, ruta FROM $tabla ORDER BY orden ASC");
    $stmt->execute();
    return $stmt->fecthAll();
    $stmt->close();
}
    
asked by Mario A. Claymation 06.09.2018 в 13:25
source

1 answer

2

Place the ajax with the async: false attribute, that will make them insert one by one in order, I think that should help you solve your problem:

$.ajax({

        url:"PATH TO AJAX FILE",
        method: "POST",
        data: actualizarOrden,
        cache: false,
        contentType: false,
        processData: false,
        async: false,
...

This makes the next photo or image that you want to upload wait for the previous one to be saved.

You let me know how it went.

    
answered by 06.09.2018 / 13:59
source