doukuanghuan7582 2016-07-04 23:09
浏览 108
已采纳

PHP多维父子数组 - 某些元素被覆盖

I am having real trouble generating a parent-child tree out of two database tables.

It's meant to reference folders and files inside them.

I am almost there, the code below generates the tree (sourced from here), but any files that are assigned to a category that contains sub-categories- they don't show..

How can I make them show?

Here is what is currently happening enter image description here

Here is what I am wanting to happen enter image description here

I believe the issue is with function Generate_Tree_Of_Categories($Tree_Data).

function Generate_Tree_Of_Categories(array $elements, $parentId = "NONE")
{
    $branch = array();

    foreach ($elements as $element) {
        if ($element['child_of'] == $parentId) {
            $children = Generate_Tree_Of_Categories($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;

            }
            $branch[] = $element;
        } else {
          $element = null;
        }
    }

    return $branch;
}

Here are my DB tables:

**Categories table:**
Reference       Title                   Type        Child_Of    Status
CAT202480   Software                    CATEGORY    NONE        ACTIVE
CAT893984   Product Manuals             CATEGORY    NONE        ACTIVE
CAT384594   Manufacturing               CATEGORY    NONE        ACTIVE
CAT394858   Manufacturing Manuals       CATEGORY    CAT384594   ACTIVE
CAT394811   Videos                      CATEGORY    NONE        ACTIVE
CAT111338   Product Videos              CATEGORY    CAT394811   ACTIVE
CAT339844   Commissioning Software      CATEGORY    CAT202480   ACTIVE
CAT339845   Release Versions            CATEGORY    CAT339844   ACTIVE
CAT339846   Beta Versions               CATEGORY    CAT339844   ACTIVE

**Files Table:**
Reference       Type        Title                       Category_Ref    Format  Status
FILE001393804   SOFTWARE    Beta software v0.9.3            CAT339846   ZIP     AVAILABLE
FILE001984843   DOCUMENT    Product A Installation Manual   CAT893984   PDF     AVAILABLE
FILE009039742   DOCUMENT    Product A Commissioning Guide   CAT893984   PDF     AVAILABLE
FILE683579248   DOCUMENT    Product A User Guide            CAT893984   PDF     AVAILABLE
FILE001393805   SOFTWARE    Product A software Release 1.9  CAT339845   ZIP     AVAILABLE
FILE001393803   SOFTWARE    Product Z program               CAT339844   ZIP     AVAILABLE

Here is an SQL dump

Here's my code for converting the above data into JSTree compatible arrays, which I later json_encode.

function Load_Downloads_TreeView()
{
    require '../../global/session_manager.php';
    require_once '../../permissions/permissions.php';

    // Determine the permissions for the current user
    $User_Permissions = Get_Permissions_SpecificUser($LoggedInUserReference, $LoggedInFlag);


    $Response = Retrieve_All_Download_Categories_SpecificStatus("ACTIVE", $LoggedInFlag);
    // $Response = Retrieve_All_Download_Categories($LoggedInFlag);
    if ($Response['Decision'] == TRUE) {
        $Download_Categories = $Response['Value'];
    } else {
        echo $Response['Notification'];
        exit();
    }


    // $Response          = Retrieve_Specific_Download_Category("CAT339845", $LoggedInFlag);
    // $Specific_Category = $Response['Value'];

    $Tree_Data = array();
    foreach ($Download_Categories as $Current_Category) {


              // This code returns an array of files assigned to the $Current_Category reference
              $Response = Retrieve_Files_In_Download_Category($Current_Category['Reference'], $LoggedInFlag);
              $Files    = $Response['Value'];
              $Child_Nodes = NULL;
              if (count($Files) > 0) {
                 $Child_Nodes = Return_Files($Files);
              }

            //$Child_Nodes would be filled with the array of files (array of arrays).

            // $Child_Nodes = NULL; //Until I figure out how to nest this information in a multidimensional array with categories

            $Response = Generate_Folder_Node($Current_Category['Reference'],$Current_Category['Name'],$Current_Category['Child_Of'], $Child_Nodes);

            $Tree_Data[] = $Response;

    }

    $Generated_Tree = Generate_Tree_Of_Categories($Tree_Data);

    echo json_encode($Generated_Tree);


}



function Generate_Folder_Node($Element_ID,$Element_Name,$Child_Of, $Child_Nodes) {
  if (!defined('id')) define('id', 'id');
  if (!defined('text')) define('text', 'text');
  if (!defined('type')) define('type', 'type');
  if (!defined('child_of')) define('child_of', 'child_of');
  if (!defined('children')) define('children', 'children');
  if (!defined('state')) define('state', 'state');
  if (!defined('opened')) define('opened', 'opened');
  $Node_Open_Array = array(opened=>"false");

  $Generated_Node = array(id=> $Element_ID, text=>$Element_Name, type=>"folder", child_of=>$Child_Of, children=>$Child_Nodes, state=>$Node_Open_Array);

return $Generated_Node;

}



function Return_Files($Child_Array)
{
  $All_Nodes = array();

    foreach ($Child_Array as $Current_Child_Element) {
        $Element_ID   = $Current_Child_Element['Reference'];
        $Element_Name = $Current_Child_Element['Name'];


        if ($Current_Child_Element['Status'] == "AVAILABLE") {
            $DisabledState = "false";
        } else if ($Current_Child_Element['Status'] == "UNAVAILABLE") {
            $DisabledState = "true";
        }

        if ($Current_Child_Element['File_Type'] == "PDF") {
            $Icon = "fa fa-file-pdf-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "ZIP") {
            $Icon = "fa fa-file-zip-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "JPG" || $Current_Child_Element['File_Type'] == "PNG") {
            $Icon = "fa fa-file-picture-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "XLS" || $Current_Child_Element['File_Type'] == "XLSX") {
            $Icon = "fa fa-file-excel-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "DOC" || $Current_Child_Element['File_Type'] == "DOCX") {
            $Icon = "fa fa-file-word-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "PPT" || $Current_Child_Element['File_Type'] == "PPTX") {
            $Icon = "fa fa-file-powerpoint-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "EXE") {
            $Icon = "fa fa-desktop fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "AVI" || $Current_Child_Element['File_Type'] == "MP4") {
            $Icon = "fa fa-file-video-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "MP3" || $Current_Child_Element['File_Type'] == "WAV") {
            $Icon = "fa fa-file-sound-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "TXT") {
            $Icon = "fa fa-file-text-o fa-lg text-inverse";
        } else if ($Current_Child_Element['File_Type'] == "HEX" || $Current_Child_Element['File_Type'] == "BIN") {
            $Icon = "fa fa-file-code-o fa-lg text-inverse";
        } else {
            $Icon = "fa fa-file fa-lg text-inverse";
        }

      $Generated_Node = Generate_File_Node($Element_ID,$Element_Name,$Icon,$DisabledState);

      if (is_array($Generated_Node) == true) {
        if (!empty($Generated_Node)) {
          array_push($All_Nodes, $Generated_Node);

        }
      }


      // $All_Nodes[] = $Child_Node;
    }

    return $All_Nodes;

}

function Generate_File_Node($Element_ID,$Element_Name,$Icon,$DisabledState) {

if (!defined('id')) define('id', 'id');
if (!defined('text')) define('text', 'text');
if (!defined('type')) define('type', 'type');
if (!defined('icon')) define('icon', 'icon');
if (!defined('state')) define('state', 'state');
if (!defined('opened')) define('opened', 'opened');
$Node_Open_Array = array(opened=>$DisabledState);

$Generated_Node = array(id=> $Element_ID, text=>$Element_Name, type=>"file", icon=>$Icon, state=>$Node_Open_Array);


return $Generated_Node;

}
  • 写回答

4条回答 默认 最新

  • dongya9346 2016-07-05 18:56
    关注

    Here is my code. There is probably a better way. I actually removed one of your functions and incorporated it into the other function:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" /> 
            <title>
            </title>
            <meta name="generator" content="BBEdit 11.5" /> 
        </head>
        <body>
    <?php
    $link = mysqli_connect("127.0.0.1", "root", "root", "stackoverflow");
    
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    $sql="SELECT * from Downloads_Categories ORDER BY id";
    $result=mysqli_query($link,$sql);
    $Download_Categories = mysqli_fetch_all($result,MYSQLI_ASSOC);
    
    $FolderTree = Generate_Tree_Of_Categories($Download_Categories);
    usort($FolderTree, function ($item1, $item2) {
        if ($item1['id'] == $item2['id']) return 0;
        return $item1['id'] < $item2['id'] ? -1 : 1;
    });
    
    echo "<pre>" . json_encode($FolderTree, JSON_PRETTY_PRINT) . "</pre>";
    
    mysqli_close($link);
    
    function Generate_Tree_Of_Categories(array $elements, $parentId = "NONE")
    {
        global $link;
        $branch = array();
        foreach ($elements as $element) {
            if ($element["Child_Of"] == $parentId) {
                $sql="SELECT * from Downloads_Files as DF WHERE '" . $element['Reference'] . "' = DF.Child_Of";
                $result=mysqli_query($link,$sql);
                $Files = mysqli_fetch_all($result,MYSQLI_ASSOC);
                $Child_Nodes = NULL;
    
                if (count($Files) > 0) {
                     $Child_Nodes = Return_Files($Files); 
                }
                $children = array_merge(Generate_Tree_Of_Categories($elements, $element['Reference']), Return_Files($Files));
    
                if ($children) {
                    $element += ['children' => $children];
                }
                else {
                    $element += ['children' => "NONE"];
                }
    
                $node = Generate_Folder_Node($element["id"],$element["Name"],$element["Child_Of"], $element["children"]);
                $branch[]=$node;
            }   
        }
        return $branch;
    }
    
    function Generate_Folder_Node($Element_ID,$Element_Name,$Child_Of, $Child_Nodes) {
    
        if (!defined('id')) define('id', 'id');
        if (!defined('text')) define('text', 'text');
        if (!defined('type')) define('type', 'type');
        if (!defined('Child_Of')) define('Child_Of', 'Child_Of');
        if (!defined('children')) define('children', 'children');
        if (!defined('state')) define('state', 'state');
        if (!defined('opened')) define('opened', 'opened');
        $Node_Open_Array = array(opened=>"false");
        $Generated_Node = array(id=> $Element_ID, text=>$Element_Name, type=>"folder", Child_Of=>$Child_Of, children=>$Child_Nodes, state=>$Node_Open_Array);
        return $Generated_Node;
    }
    
    
    
    function Return_Files($Child_Array) {
    
        $All_Nodes = array();
    
        foreach ($Child_Array as $Current_Child_Element) {
            $Element_ID   = $Current_Child_Element['Reference'];
            $Element_Name = $Current_Child_Element['Name'];
    
            if ($Current_Child_Element['Status'] == "AVAILABLE") {
                $DisabledState = "false";
            } else if ($Current_Child_Element['Status'] == "UNAVAILABLE") {
                $DisabledState = "true";
            }
    
            if ($Current_Child_Element['File_Type'] == "PDF") {
                $Icon = "fa fa-file-pdf-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "ZIP") {
                $Icon = "fa fa-file-zip-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "JPG" || $Current_Child_Element['File_Type'] == "PNG") {
                $Icon = "fa fa-file-picture-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "XLS" || $Current_Child_Element['File_Type'] == "XLSX") {
                $Icon = "fa fa-file-excel-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "DOC" || $Current_Child_Element['File_Type'] == "DOCX") {
                $Icon = "fa fa-file-word-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "PPT" || $Current_Child_Element['File_Type'] == "PPTX") {
                $Icon = "fa fa-file-powerpoint-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "EXE") {
                $Icon = "fa fa-desktop fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "AVI" || $Current_Child_Element['File_Type'] == "MP4") {
                $Icon = "fa fa-file-video-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "MP3" || $Current_Child_Element['File_Type'] == "WAV") {
                $Icon = "fa fa-file-sound-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "TXT") {
                $Icon = "fa fa-file-text-o fa-lg text-inverse";
            } else if ($Current_Child_Element['File_Type'] == "HEX" || $Current_Child_Element['File_Type'] == "BIN") {
                $Icon = "fa fa-file-code-o fa-lg text-inverse";
            } else {
                $Icon = "fa fa-file fa-lg text-inverse";
            }
    
          $Generated_Node = Generate_File_Node($Element_ID,$Element_Name,$Icon,$DisabledState);
    
          if (is_array($Generated_Node) == true) {
            if (!empty($Generated_Node)) {
              array_push($All_Nodes, $Generated_Node);
            }
          }
        }
        return $All_Nodes;
    }
    
    function Generate_File_Node($Element_ID,$Element_Name,$Icon,$DisabledState) {
    
        if (!defined('id')) define('id', 'id');
        if (!defined('text')) define('text', 'text');
        if (!defined('type')) define('type', 'type');
        if (!defined('icon')) define('icon', 'icon');
        if (!defined('state')) define('state', 'state');
        if (!defined('opened')) define('opened', 'opened');
        $Node_Open_Array = array(opened=>$DisabledState);
    
        $Generated_Node = array(id=> $Element_ID, text=>$Element_Name, type=>"file", icon=>$Icon, state=>$Node_Open_Array);
    
        return $Generated_Node;
    }
    ?>
        </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题