L
L
Luchifer2018-03-30 09:40:00
Yii
Luchifer, 2018-03-30 09:40:00

How to disable array sorting?

Hello, I have such a problem, I change the number of goods with ajaxom, everything works, but the array is sorted according to itself as it wants,
I take the goods from the session according to this orderby for the base, it did not help
this first state
5abddad7b0206030808183.jpeg
after changing the number of goods, it turns out, the goods that were on 1 ohm place now on 3 ohm
5abddafeb022f313245342.jpeg
it's buttons

<div class="input-group">
                            <input id="<?= $id ?>"  type="text" class="form-control input-number quantity" value="<?= $item['qty'] ?>">
                            <a href="cart" data-id="<?= $id ?>" class="btn btn-default addd"><i class="fas fa-sync"></i></a>
                        </div>

this is an ajax request
$('.addd').on('click', function () {
        var id = $(this).data('id');
        qty = $('input[id=' + id + ']').val();
        $.ajax({
            url: '/site/addd',
            data: {id: id , qty: qty},
            type: 'GET'
        });
    });

this is a function
public function actionAddd()
    {
        $id = Yii::$app->request->get('id');
        $qty = (int)Yii::$app->request->get('qty');
        $qty = !$qty ? 1 : $qty;
        $product = Products::findOne($id);
        if(empty($product)) return false;
        $session = Yii::$app->session;
        $session->open();
        $cart = new Cart();
        $cart->recalc($id);
        $cart->addToCart($product, $qty);
        return $this->render('cart', compact('session'));
    }


what is the problem ?(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
serginhold, 2018-03-30
@serginhold

By turning on psychic abilities:
or
if sorting and output to us in php, and the goods in the session are in some specific order, then
order by field(`id`,7, 12, 4) # your id order
or if the list output is in js,
see js array before output, perhaps the keys are id, so it is necessary to apply array_values ​​before output to json

A
Adamos, 2018-03-30
@Adamos

If you output some data to the page in JSON, then any arrays that have numeric keys will be sorted by these keys. Regardless of how you sorted them before JSON was formed.
The easiest way is not to use arrays with keys in principle. Need id - make it a property, not a key. And everything will reach the page in the same order in which you created it. If they are not needed at all, output array_values ​​in JSON, as already mentioned.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question