Question

I want to know how to update my database based on this type of array:

Array
(
    [0] => Array
        (
            [id] => 58
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 62
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 61
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 60
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 63
                        )

                )

        )

    [2] => Array
        (
            [id] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 59
                        )

                )

        )

)

I tried different methods but it's not working as it should.

Here's my function that saved the new order of my list, I use nestedSortable('toHierarchy')

function order()
{
    $list = $_POST['list'];
    foreach ($list as $key => $value) {
        if(is_array($value['children'])) {
            $i=1;
            foreach($value['children'] as $k=>$v) {
                $this->section->edit(array('order' => $i, 'parent_id'=>$value['id']), 
                                     "WHERE `id`=" . $v['id']);
                echo 'parent '.$value['id'].'child->'.$v['id'].' order->'.$i.'<br/>';
                $i++;
            }
        }
        else {
            $this->section->edit(array('order' => $key, 'parent_id'=>0), 
                                 "WHERE `id`=" . $value['id']);
        }
    }
}

But it's not working for list with more than 2 levels.

this is the js i'm using

$('ol.sortable').nestedSortable({
         update : function () {
var orderNew = $(this).nestedSortable('serialize', {startDepthCount: 0});
//alert(orderNew);
        $.ajax({
            type: 'post',
            url: '<?php echo (APP_URL); ?>projects/<?php echo $project_id; ?>/sections/order_list',
            data: {list:orderNew}
            });             
},
            disableNesting: 'no-nest',
            forcePlaceholderSize: true,
            handle: 'div',
            helper: 'clone',
            items: 'li',
            maxLevels: 4,
            opacity: .6,
            placeholder: 'placeholder',
            revert: 250,
            tabSize: 25,
            tolerance: 'move',
            toleranceElement: '> div'
        });
Était-ce utile?

La solution

You could implement a recursive function to deal with unlimited nesting levels. But I suggest as an alternative to use the nested sortable serialize method:

$('your_sortable_selector').nestedSortable('serialize');

This generates a flat array with the keys being the item id and the values for each key being the parent id. You can then implement a less complicated function like so:

function order()
{
    $list = $_POST['list'];

    // an array to keep the sort order for each level based on the parent id as the key
    $sort = array();
    foreach ($list as $id => $parentId) {
        /* a null value is set for parent id by nested sortable for root level elements
           so you set it to 0 to work in your case (from what I could deduct from your code) */
        $parentId = ($parentId === null) ? 0 : $parentId;

        // init the sort order value to 1 if this element is on a new level
        if (!array_key_exists($parentId, $sort)) {
            $sort[$parentId] = 1;
        }

        $this->section->edit(array('order' => $sort[$parentId], 'parent_id' => $parentId), "WHERE `id`= $id");

        // increment the sort order for this level
        $sort[$parentId]++;
    }
}

This should work just as well for any number of nesting levels.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top