Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
10.2k views
in Technique[技术] by (71.8m points)

php - Renumbered from n to 1 in ajax

I am developing a prestashop module to position products in a brand. I created a table with id_product - position and id_manufacturer. In each brand currently I have a product in position 0 and other with positions greater than 0. What I am trying to do in my module is when I move a product, the products that are at 0 should be available. increments from 0 to n and that my product that I automatically move this puts at the requested position.

This is what my product list looks like list

I wish that the sort would be suddenly from the largest to the smallest. Here is my JS code:

$('.admindraganddropproducts #list_products tbody').sortable({
    start: function(e, ui) {
        $(this).attr('data-previndex', ui.item.index());
    },
    update: function (event, ui) {
        var list = ui.item.parent('tbody');
        var pos = $('tbody.ui-sortable > tr').size();
        var element_id = ui.item.attr('id');
        var movement = ui.position.top - ui.originalPosition.top > 0 ? 1 : 0;

        $(this).removeAttr('data-previndex');

        $(list.find('tr').each(function () {
            pos--;
            $(this).find('input.position_product_manufacturer').val(pos);
        }));

        $('.loading_products').css('display', 'block');
        $.ajax({
            url: draganddrop_manufacturer_products,
            type: 'post',
            async: true,
            dataType: "json",
            data: {
                ajax: true,
                controller: 'AdminDrag',
                action: 'Position',
                id_manufacturer: $('#manufacturer_select option:selected').val(),
                id_product: $('#' + element_id + ' input.id_product').val(),
                way: movement,
                position: $('#' + element_id + ' input.position_product_manufacturer').val(),
            },
            success: function (data) {
                $('.loading_products').css('display', 'none');
                if (!data.error) {
                    $('#list_products').before('<div class="alert alert-success">' + data.message + '</div>');
                } else {
                    $('#list_products').before('<div class="alert alert-danger">' + data.message + '</div>');
                }

                var id_manufacturer_url = GetURLParameter('id_manufacturer');
                if (!id_manufacturer_url) {
                    window.location.href = window.location.href + '&id_manufacturer=' + $('#manufacturer_select option:selected').val();
                } else {
                    location.reload();
                }
            }
        });
    }
});

when I move the element the repositioning is done correctly but it is php side where I get stuck. Here is my PHP:

public function updatePositionProductManufacturer($way, $position, $id_manufacturer)
{
    $res = Db::getInstance()->executeS('
        SELECT mp.`id_product`, mp.`position`, mp.`id_manufacturer`
        FROM `' . _DB_PREFIX_ . 'manufacturer_product` mp
        WHERE mp.`id_manufacturer` = ' . (int) $id_manufacturer . '
        ORDER BY mp.`position` DESC');
    if (!$res) {
        return false;
    }

    $movedProduct = false;
    foreach ($res as $product) {
        if ((int) $product['id_product'] == (int) $this->id_product) {
            $movedProduct = $product;
        }
    }

    if ($movedProduct === false) {
        return false;
    }
    
    // < and > statements rather than BETWEEN operator
    // since BETWEEN is treated differently according to databases
    $result = (Db::getInstance()->execute('
        UPDATE ' . _DB_PREFIX_ . 'manufacturer_product mp
        SET mp.position = mp.position ' . ((int)$way ? '- 1' : '+ 1') . '
        WHERE mp.position
        ' . ((int)$way
                ? '> ' . (int) $movedProduct['position'] . ' AND mp.position <= ' . (int) $position
                : '< ' . (int) $movedProduct['position'] . ' AND mp.position >= ' . (int) $position) . '
        AND mp.id_manufacturer =' . (int) $movedProduct['id_manufacturer'])
        && Db::getInstance()->execute('
        UPDATE ' . _DB_PREFIX_ . 'manufacturer_product mp
        SET mp.position = ' . (int) $position . '
        WHERE mp.id_manufacturer = ' . (int) $movedProduct['id_manufacturer'] . '
        AND mp.id_product =' . (int) $movedProduct['id_product']));

    return $result;
}

it doesn't work at all.

Do you have an idea of ??the problem.

Thank you for your help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...