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
Here is what I am wanting to happen
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;
}