【问题标题】:Save array positions保存数组位置
【发布时间】:2011-09-25 18:21:27
【问题描述】:

我有一棵树,用户可以在其中拖放元素并更改元素位置。 每次更改后,我都会发送这个序列化数组并且我想保存它。

示例树:

Economics
Blogging
General Dev
Japan
Productivity

    Humanities
        Education
        Science
            Haskell
                Earth
            PHP

我通过 ajax 发送序列化树,所以我有数组链接。

'0' => "1"
'1' => "2"
'2' => "3"
'3' => "4"
'4' ...
    '0' => "5"
    '1' ...
        '0' ...
            '0' => "6"
            '1' ...
                '0' => "7"
                '1' ...
                    '0' => "8"
                    '1' ...
                        '0' ...
                            '0' => "9"
                            '1' ...
                                '0' => "10"
                        '1' ...
                            '0' => "11"

数组值为表格行id。

如何将此数组以正确的位置插入到数据库中?

【问题讨论】:

    标签: php mysql arrays tree


    【解决方案1】:

    我建议使用传统的树存储方法:

    create table catalogue (
        id int not null auto_increment primary key,
        name varchar(255),
        catalogue_id int,
        ord int
    );
    

    所以首先你需要正确填充db,然后休息应该很容易处理。

    实际代码。

    客户端:

       <ul id="enclosure_0" class="sortable">
            <li id="item_1">Level0 - Item 1</li>
            <li id="item_2">Level0 - Item 2</li>
            <li id="item_3">Level0 - Item 3</li>
            <li id="item_4">Level0 - Item 4</li>
            <li id="item_5">Level0 - Item 5
                <ul id="enclosure_5" class="sortable">
                    <li id="item_6">Level1 - Item 1</li>
                    <li id="item_7">Level1 - Item 2</li>
                    <li id="item_8">Level1 - Item 3</li>
                    <li id="item_9">Level1 - Item 4</li>
                </ul>
            </li>
       </ul>        
       <script>     
           $(document).ready(function(){
               $(".sortable").sortable({
                   stop: function(ev, ui){
                       var list = $(this).sortable("toArray");
                       $.get("sort.php", {items: list, parent_id: $(this).attr('id')}, function(data){window.alert("stored");}); 
                    }
                });
            });
        </script>
    

    服务器端(sort.php):

    <?php
    
    /* you have new order in $_GET["items"], you have parent id in $_GET["parent_id"] */
    
    /* easiest way:
     * */
    
    preg_match("/enclosure_([0-9])+/", $_GET["parent_id"], $tmp);
    $parent_id = (int) $tmp[1];
    $counter = 0;
    foreach ($_GET["items"] as $item){
        $counter++;
        preg_match("/item_([0-9]+)/", $item, $tmp);
        $item_id = (int) $tmp[1];
        $db->query("update catalogue set ord = $counter where id = $item_id");
        /* alternatively, somewhat safer */
        $db->query("update catalogue set ord = $counter where id = $item_id and catalogue_id = $parent_id");
    } 
    

    注释和假设:

    1) 顶级类别的 catalogue_id 应为 0
    2)我相信你足够聪明,可以让客户端动态呈现。 3) 假设 ord 是基于 1 的。 4) php 未测试,但应该没问题

    【讨论】:

    • 是的,对我来说最有趣的部分是这个 php 代码,但我认为它是错误的,因为它应该递归地保存位置。像这样它仅适用于两个级别的可排序。如果我们有更多关卡呢?
    • 没有。检查我的客户端脚本。它仅将更改的级别保存到后端。每次你改变它。每次重新排序所有树更有效。
    • 对不起,我错过了这个。谢谢你。它简单而更好的解决方案。
    【解决方案2】:

    我不会尝试以正确的顺序保持 DB 表中的行,我会在表中添加一个“位置”列。我假设您已经有一个“父”列来保留层次结构。

    当用户重新排列项目时,只需更新位置列。

    【讨论】:

    • 是的,我在数据库中有“位置”和“父”列。我不知道如何通过这个数组并正确保存到数据库。服务器站点。
    猜你喜欢
    • 2014-03-31
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2015-11-17
    • 2012-06-16
    • 2021-10-11
    • 2010-10-13
    相关资源
    最近更新 更多