【问题标题】:Making A List Using PHP And NestedSortable使用 PHP 和 NestedSortable 创建列表
【发布时间】:2013-02-16 16:53:16
【问题描述】:

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

好吧,我是个白痴,那天晚些时候我回到家时坐下来,拿了我的本地副本,添加了一些内容并将其上传到服务器......没有意识到我忘记下载更新的版本!所以噗,我朋友的所有工作都消失了,我被导航调整卡住了。

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

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

(请注意数组值。)

这就好像父母只有一个孩子:

这就像一个孩子有两个孙子:

这是混合的:

我的 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(父)、父(子)和孙子。

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

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

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

【问题讨论】:

    标签: php jquery mysql nested-sortable


    【解决方案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";
        }
        ?>
    

    【讨论】:

    • 酷@KentonDeJong !我要构建一个递归函数......快乐编码:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2017-08-04
    • 2023-03-14
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多