Converting Mysql Select to Multidimensional Array of Directory Structure independent of levels using PHP -
so have select statement returns 3 values db:
- folder id,
- folder name,
- parent id.
if folder has no parent returns null parent id.
using data have split results 2 arrays.
one named $tree
, used "directory tree", , firstly contain parent folders have parent id set null... , named $children
, contains other folders children.
with these attempting create multidimensional array($tree)
use display file structure user using php/html. ideally use recursion allow not knowing how deep directory goes.
i trying following no success , after whole day thinking feel stuck (function find children gets called function getdirtree further down):
// should recursive function. takes 2 arrays argument. // returns $tree() multiple dimension array function findchildren($tree, $children){ // tree has 2 parents in first run foreach($tree $folder){ $temparray = array(); // children has 4 foreach($children $child){ if ($child['parentid'] === $folder['folderid']){ array_push($temparray, $child); if(($childkey = array_search($child, $children)) !== false) { unset($children[$childkey]); } } } if(($parentkey = array_search($tree, $folder)) !== false) { array_push($children[$parentkey],$temparray); } } // need have sort of recursion in function // if (!empty($children)){ // findchildren($tree, $children); // } } // takes userid int , returns multi dimensional array representing users folder structure. function getdirtree($userid){ global $mysqli; $children = array(); $tree = array(); if($folders = $mysqli->prepare("select folders.id, folders.name, child_of_folder.parent_id child_of_folder right join folders on (child_of_folder.child_id = folders.id) folders.user_id = ?;")) { // bind parameters... s string , variable $name bound. if ($folders->bind_param("i", $userid)) { // execute query if ($folders->execute()) { // store results if($folders->store_result()){ // bind results if($folders->bind_result($folderid, $foldername, $parentid)) { // fetch results while ($folders->fetch()) { if ($parentid === null) { array_push($tree, array('folderid' => $folderid, 'foldername' => $foldername, 'parentid' => $parentid)); } else { array_push($children, array('folderid' => $folderid, 'foldername' => $foldername, 'parentid' => $parentid)); } } } else { $hasfolder = null; } } else { // if there no values store return false $hasfolder = null; } } else { // if there problem executing statement return null $hasfolder = null; } } else { // if there problem binding statement return null $hasfolder = null; } } else { // if there problem preparing statement return null $hasfolder = null; } if(!empty($children)){ findchildren($tree, $children); } $folders->free_result(); $mysqli->close(); return $tree; }
output of $tree
, $children
before being passed findchildren()
:
children array before findchildren array ( [0] => array ( [folderid] => 2 [foldername] => home [parentid] => 1 ) [1] => array ( [folderid] => 3 [foldername] => account [parentid] => 2 ) [2] => array ( [folderid] => 4 [foldername] => bill [parentid] => 2 ) [3] => array ( [folderid] => 6 [foldername] => work [parentid] => 2 ) ) tree array before findchildren array ( [0] => array ( [folderid] => 1 [foldername] => work [parentid] => ) [1] => array ( [folderid] => 5 [foldername] => hello [parentid] => ) )
i think should work. changed xml structure instead of arrays since xml structure easier handle espcially when doing recursive. have taken assumptions, example $children array contains children in db. hope were looking for.
function getfolderstructure($userid) { //global variable $xml = new domdocument(); $xml->formatoutput = true; $element = $xml->createelement($userid); $element->nodevalue = ''; //get top level folders. getdirtree($userid); //goes through top level folders. foreach($tree $toplevelfolder) { findchildren($element, $toplevelfolder); } $xml->appendchild($element); return $xml; } //prob need use $this->xml function findchildren(&$element, $folder) { if(is_array($folder) && isset($folder["folder_name"]) && isset($folder["folderid"])) { $folder = $xml->createelement($folder["folder_name"], ""); $folderidattribute = $xml->createattribute("folderid"); $folderidattribute->value = $folder["folderid"]; $folder-appendchild($folderidattribute); if(isset($folder["parentid"]) && !empty($folder["parentid"])) { $parentidattribute = $xml->createattribute("parentid"); $parentidattribute->value = $folder["parentid"]; $folder->appendchild($folder); } foreach(findchildrenarray($folder['folderid']) $child) { findchildren($folder, $child); } $element->appendchild($folder); } } function findchildrenarray($folderid) { $retarray = array(); foreach($children $child) { if(isset($child["parentid"]) && $child["parentid"] == $folderid) { array_push($retarray, $child); } } }
Comments
Post a Comment