2012-09-24 58 views
0

我正在使用EXT JS構建一個簡單的樹,它將顯示單個目錄及其內容。無論出於何種原因,我經歷了所有的例子,無法複製我想要的東西。我想創建一個基於目錄結構的JSON對象,以便它可以輸入到EXT JS樹中。EXT JS - 使用PHP獲取目錄樹結構到JSON對象中

這是我的腳本,打開目錄,創建父節點,然後嘗試創建子節點。在這個目錄裏面只有.xml文件。我已經得到它與打開目錄,只是創建文件的節點,但它不顯示父級或根級別,並失去了漂亮的崩潰影響。

這裏是我的代碼:

if($handler = opendir($dir."/$market_desc")) 
      { 

      while (($sub = readdir($handler)) !== FALSE) 
      { 
       if ($sub !== "." && $sub !== ".." && $sub !== ".svn") 
       { 
        if(is_file($dir."/$market_desc/".$sub)) 
        { 
         $subDir[] = array(
         'text' => $sub, 
         'id' => $sub, 
         'checked' => false, 
         'leaf' => true, 
         'cls' => 'file' 
          ); 
        } 
       } 

      } 

      $listDir[] = array(
      'text' => $market_desc, 
      'id' => $market_desc, 
      'checked' => false, 
      'cls' => 'folder', 
      'children' => array($subDir) 
     ); 


      closedir($handler); 
      unset($handler); 

的目錄結構只到達1級深,在根級別的一些文件,接着由多個目錄也包含文件,但永遠不會超過1個水平下降。

我是通過貿易後端PHP開發人員,所以我對我的noobness道歉,當涉及到的JavaScript和EXT

+0

問題真的是這樣嗎? –

+0

那麼問題是,我使用的腳本出了什麼問題,因爲它不是將文件嵌套到文件夾中.... – xXPhenom22Xx

+0

總之,我需要一個簡單的php腳本來瀏覽目錄並創建正確的數組結構它可以被json_encoded。該目錄永遠不會超過1級。 – xXPhenom22Xx

回答

0

需要一些試驗和錯誤後形成正確此JSON的我在這個PHP腳本EXT JS的正確格式。它會打開目錄並構建嵌套數組,它可以被json_encoded併發送到EXT JS Tree進行渲染

  // Get market specific features and make nodes 
     if($handler = opendir($dir."/$market_desc")) 
     { 
      while (($sub = readdir($handler)) !== FALSE) 
      { 
       if ($sub !== "." && $sub !== ".." && $sub !== ".svn") 
       { 
        if(is_file($dir."/$market_desc/".$sub)) 
        { 
           $subDir[] = array(
           'text' => $sub, 
           'id' => $sub, 
           'checked' => false, 
           'leaf' => true, 
           'parent' => $market_desc, 
           'children' => NULL, 
           'cls' => 'file' 
           ); 
        } 

       } 

      } 
          $listDir[] = array(
          'text' => $market_desc, 
          'id' => $market_desc, 
          'checked' => false, 
          'cls' => 'folder', 
          'parent' => NULL, 
          'expanded' => true, 
          'children' => $subDir 
          ); 


      closedir($handler); 
      unset($handler); 
     }