使用PHP和NestedSortable创建列表

时间:2013-03-02 03:41:54

标签: php jquery mysql nested-sortable

我正在尝试制作自己的CMS,我想做的一件事是这样做,以便我可以重新排序并更改CMS中的导航元素。我发现了一个名为NestedSortable的插件让我这样做。我使用它并且遇到了很多麻烦,所以我得到了我的专家PHP朋友来帮我做。她坐下来,挣扎了一下,然后开始工作。

好吧,我是白痴,当我晚些时候回到家时,我坐下来,拿了我的本地副本,添加了一些内容并将其上传到服务器......没有意识到我忘了下载更新的版本!噗噗噗来,我朋友的所有工作都已消失,而且我的导航调整无法正常工作。

我丢失了index.php(显示在页面上)文件,但我保存了nav_adjust.php(将其输入数据库)。我想要的是能够重新排序导航并将其发送到数据库以便在刷新时正确显示。按下提交按钮后,信息将发送到nav_adjust.php。

唯一的限制是导航只能有3个级别(主要,子级和孙级)。我的导航看起来就像他们都是主要的。我已经回复了所有图像的回声,因此您可以看到nav_adust.php正在做什么:

(请注意数组值。)

All parents, no children

这就像父母一个孩子一样:

1 child on a parent

就好像孩子有两个孙子一样:

Two grandchildren on a child

这是混合的:

Mixed family of children, grandchildren and such

我的index.php文件不再起作用,如下所示:

<form method="post" action="nav_adjust.php"> <!--This form posts to nav_adjust.php-->
    <ol id="pageList">
    <?php 
    $nav = mysql_query("SELECT * FROM `".$name['Website']);
    $num = 0;
    while( $col_nav = mysql_fetch_assoc($nav) ){
        if ($col_nav['orderId'] != 0) {
            echo "<li id='list_".$col_nav['id']."'>
            <div>Example Title</div>";  
        } else {
            echo "<ol>
                <li id='list_".$col_nav['id']."'>
                    <div><h1>Example Title</div>";
                </li>
            </ol>\n";
        }
        echo "</li>\n";
        ++$num;
    }
    ?>
    </ol>
    <button type='submit'>Update</button>
    </form>

<script>

$(document).ready(function(){

    $('#pageList').nestedSortable({
            /*option that don't matter*/
            });

    $("button").on('click', function(event){
        serialized = $('ol#pageList').nestedSortable('serialize');
          $.post( "nav_adjust.php", serialized, function( data ) {
            alert( data );
            });
        return false;
    });

});

 </script>

同样,上面的代码不起作用。而不是使列表项看起来像这样:

<ol>
<li>
    <ol>
    <li></li>
    ...
    </ol>
 </li>
 ...
 </ol>

它看起来像这样(注意ol之前的封闭li标签):

<ol>
<li></li>
<ol>
    <li></li>
</ol>
...
</ol>

这是我的朋友为我做的nav_adjust.php,如果从index.php获得正确的信息,它可以很好地工作

$list = $_POST['list'];
$num = 1;
foreach ($list as $key => $value ) {
    if ($value == "null") {
        //$value = $num;
        mysql_query("UPDATE `myTable` SET orderId=".$num.", parent='null' WHERE id=".$key."");
        $text .= "UPDATE `myTable` SET orderId=".$num.", parent='null' WHERE id=".$key."\n\n";
    } else {
                $query = "UPDATE `myTable` SET orderId=". $num .", parent=".$value;
                $text .= "UPDATE `myTable` SET orderId=". $num .", parent=".$value."\n\n";
                $var = $list[$value];
                if ( $var != null && $list[$var] != null ) {
                    $query .= ", grandchild = 1";
                }
                $query .= " WHERE id=".$key; 
        mysql_query($query);
    }
    $num++;
}

echo "Content is below\n";
print_r( $_POST['list']);
echo $text;

所以,作为回顾,我有一个导航,将index.php中的序列化数据提交给nav_adjust.php,然后将其提交到数据库中(然后回复它,以便我知道我做了什么)。然后,在刷新时,index.php应该保留列表项,所有子项都是子项等。但是我覆盖了index.php并且不知道我的朋友是怎么做到的。

我的表格字段是(在我忘记之前):

id,title,orderId(parent),parent(child)和grandchild。

我的意思是更改orderId和parent的名称以减少混乱,但我从未这样做过:(

如果有人能帮助我,我会非常感激。

如果有任何其他信息我可以给你,请询问!

1 个答案:

答案 0 :(得分:1)

经过几个小时的挣扎,我有一个天才的想法,就是问GoDaddy他们是否备份了文件和目录,他们做到了!所以我收回了我的档案。对于任何感兴趣的人,index.php上缺少的代码看起来像这样(其中$ name ['website']是'myTable'):

$parents = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = 0 ORDER BY `orderId` ASC");

    while($parent = mysql_fetch_array($parents)){            
        echo "<li id='list_".$parent['id']."'>
        <div>
            <span class='disclose'>
                <span></span>
            </span>
            <span class='title'>
                <a href='mod.php?id=".$parent['id']."'>".stripslashes($parent['page'])."</a>
            </span>
        </div>";    

        // Get chil elements

        $children = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = " . $parent['id'] . " ORDER BY `orderId` ASC") or die(mysql_error());
        while($child = mysql_fetch_array($children)){
            echo "<ol>
            <li id='list_".$child['id']."'>
                <div>
                    <span class='disclose'>
                        <span></span>
                    </span>
                    <span class='title'>
                        <a href='mod.php?id=".$child['id']."'>".stripslashes($child['page'])."</a>
                    </span>
                </div>";
            # Get grandchild elements
            # (Please consider making an recursive function or so out of this if you plan to have grand grand children etc.,
            # Because if you create new whiles manually for every instance, this is gonna look pretty darn ugly.
            # Also, it makes it impossible to have an unknown depth.)           
            $grandchildren = mysql_query("SELECT * FROM `".$name['Website']."` WHERE parent = " . $child['id'] . " AND grandchild = 1 ORDER BY `orderId` ASC");             
            while($grandchild = mysql_fetch_array($grandchildren)){
                echo "<ol>
                    <li id='list_".$grandchild['id']."'>
                        <div>
                            <span class='disclose'>
                                <span></span>
                            </span>
                            <span class='title'>
                                <a href='mod.php?id=".$grandchild['id']."'>".stripslashes($grandchild['page'])."</a>
                            </span>
                        </div>
                    </li>
                </ol>\n";               
            }

            // Close child
            echo "</li>
            </ol>\n";
        }
        // Close parent
        echo "</li>\n";
    }
    ?>