php - 使用 PHP 和 NestedSortable 制作列表

标签 php jquery mysql nested-sortable

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

好吧,我是个白痴,那天晚些时候回到家后,我坐下来,拿了我的本地副本,添加了一些东西并上传到服务器......没有意识到我忘记下载更新的版本!太糟糕了,我 friend 的所有工作都没有了,我被一个不起作用的导航调整困住了。

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

唯一的限制是导航只能有 3 个级别(主要、子级和孙级)。我的导航看起来像它们都是主要的。我已经对所有图像发出 echo 提醒,这样您就可以看到 nav_adust.php 正在做什么:

(请注意数组值。)

All parents, no children

这就好像 parent 有一个 child :

1 child on a parent

这就好像一个 child 身上有两个孙子:

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>

这是我 friend 为我做的 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 并且不知道我的 friend 是怎么做到的。

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

id、title、orderId(父)、父(子)和孙。

我本来打算更改 orderId 和 parent 的名称以减少混淆,但我从来没有这样做:(

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

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

最佳答案

经过几个小时的努力,我灵机一动,问 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";
    }
    ?>

关于php - 使用 PHP 和 NestedSortable 制作列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170429/

相关文章:

mysql - 将一个大表拆分为多个表

mysql - SQL可选参数?

php - 是否可以将 kartik-gridview editable 设置为 inline 而不是 popup?

php - 如何使用 PHP 回显具有条件语句的变量?

php - Symfony 5 如何在具有多对多关系的更新中自动发送当前用户

php - 如何对值求和并合并重复记录

javascript - 时刻 js 的无效日期

javascript - 工具提示指令内容未更新

javascript - 元刷新重定向页面中的 jQuery 计时器

mysql - 2 个表中的外键在第 3 个表中进行 PK