2013-03-02 18 views
2

我試圖製作自己的CMS,我想要做的一件事就是讓它可以重新排序並更改CMS中的導航元素。我找到了一個名爲NestedSortable的插件讓我這樣做。我用它並且遇到了很多麻煩,所以我讓我的專家PHP朋友幫助我做到這一點。她坐下來,掙扎了一下,然後開始工作。製作使用PHP和NestedSortable的列表

那麼,我是白癡,當我回到家後,當天我坐下來,拿着我的本地副本,添加了一些東西給它,並上傳到服務器......沒有意識到我忘記了下載最新的版!所以,我朋友的所有工作都消失了,我被一個不起作用的導航調整卡住了。

我丟失了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前的封閉L1標籤):

<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然後SUBM其精美作品將其存入數據庫(並將其反饋回來,以便我知道我做了什麼)。然後,index.php刷新後,應該保留列表項目,所有孩子都是孩子等。但我覆蓋index.php,不知道我的朋友是如何做到的。

我的表中的字段是(我忘記前):

ID,標題,訂單ID(父),父母(孩子)和孫子。

我的意思改變訂單ID和家長的名字,使其少混亂,但我從來沒有:(

如果有人能幫助我,我會這樣非常感激。

如果有是我可以給你的任何其他信息,請問!

回答

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"; 
    } 
    ?> 
+1

酷@KentonDeJong!我要建立一個遞歸函數...快樂編碼:) – 2017-03-09 07:36:57