codeigniter jquery sortable保存到数据库

问题描述:

我想在使用jquery sortable时将图片列表的顺序保存到数据库。
我觉得我很近,但不能让我的头围绕最后的细节。
我使用CI 2.1.3和jquery-ui 1.10.3。

I am trying to save the order of a list of images to the database when using jquery sortable. I feel i am very close, but cant get my head around the final details. I am working with CI 2.1.3 and jquery-ui 1.10.3.

我有一个动态生成的列表与一个图像:

I have a dynamicaly generated list with an image:

<ul id="order">
<li id="item-1"><img src="abc.jpg" /></li>
<li id="item-2"><img src="def.jpg" /></li>
<li id="item-3"><img src="ghi.jpg" /></li>
</ul>

和以下Jquery:

<script>
$(document).ready(function() {

        $( "#order" ).sortable({
            opacity: 0.6,
            cursor: 'move',

            update: function(event, ui){
 var order = $(this).sortable("serialize");
 console.log(order);

                $.ajax({
                    url: "http://localhost/gridrobin/home/save_order",
                    type: 'POST',
                    data: order,
                    success: function (data) {
                        $("#test").html(data);
                    }

                });
                }

            });

});
</script>

这个工作正常,我可以重新排序我的列表。现在我想把新订单保存到数据库。我发送ajax的帖子到控制器,它来了。我检查了一个var_dump。

This works fine and i can reorder my list. Now i want to save the new order to the database. I send the ajax post to the controller and it comes through. I checked with a var_dump.

//var_dump($_POST);

        $items = $this->input->post('item');
        $total_items = count($this->input->post('item'));

        echo '<h3>Debugging</h3>';
        echo "<p>Total items sent: $total_items</p>";

        $this->rd_model->update_order($total_items, $items);

然后我将此数据发送到我的模型:

Then I send this data to my model:

 for($item = 0; $item < $total_items; $item++ )
        {

            $data = array(
                    'id' => $items[$item],
                    'order' => $order = $item
            );

            $this->db->where('id', $data['id']);

            $this->db->update('portfolio_items', $data);
            echo '<br />'.$this->db->last_query();
        }

并回显最后一个db-query以进行调试。

And echo out the last db-query for debugging.

现在,当我切换项目1和项目2,我得到一个500内部错误。当我切换他们,我收到最后一个查询执行的回声,这似乎很好。

Now when i switch item 1 and item 2, i get a 500 internal error. When i switch them back, i receive the echo of the last query executed, which seems fine.

UPDATE `portfolio_items` SET `order` = 1 WHERE `id` = '1'
UPDATE `portfolio_items` SET `order` = 2 WHERE `id` = '2'
UPDATE `portfolio_items` SET `order` = 3 WHERE `id` = '3'



我不太明白为什么数据库更新时列表切换回其原始状态,但不是其他。

I dont quite understand why the database updates when th list is switched back to its orignial state, but not otherwise.

UPDATE
对于有相同问题的人来说,sakibmoon答案帮了我很多,但主要的问题是重复输入错误,因为显然我已经将顺序表设置为唯一索引...

UPDATE For people with the same problem, sakibmoon answer helped me a lot, but the main problem was a duplicate entry error, because apparently i had set the order table as a unique index...

问题出在你的数据数组模型。使用此更改 -

The problem is within your data array in the model. Change that with this -

$data = array(
                'id' => $items[$item],
                'order' => $item+1
        );

同时更改更新行 -

Also change the update line -

    $this->db->update('portfolio_items', $data['order']);

UPDATE

几个变化。将更新更改为 -

Couple of changes. Change the update to this-

$this->db->update('portfolio_items', array('order' => $data['order']));

如果我设置 $ config ['csrf_protection'] = FALSE config.php 。但是你应该将它设置为 TRUE 。我不知道如何做这项工作。我知道的是,你必须发送csrf_token与你的ajax调用。您应该为此创建一个单独的问题。这个问题标题意味着完全不同。

The code is working now if I set $config['csrf_protection'] = FALSE in config.php. But you should set it to TRUE. I don't know how to make that work. All I know is that you have to send csrf_token with your ajax call. You should create a separate question for that. This question title means something entirely different.