my first question. I am using Nestable List Plugin and trying to get same structure from an database.
The database looks like this (data source) (tobad need 10 reputation) This type of structure
ID: 1
ParentID: 0
Text: Hello World
Type: Text
----New row
ID: 2
ParentID: 1
Text: CHild :D
Type: text
and so on., dont know the name for it but thats one type of hierarchy structure. I am trying to convert that into something like this, with unlimited lvls: [{"id":13},{"id":14},{"id":15,"children":[{"id":16},{"id":17},{"id":18}]}]
This is my try so far
PHP Side
if($productSQL[0])
{
foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues)
{
if($productSQL[1]['objProductIDArr'][$iIDKey] == 0)
{
// Type Text
$objData = $getDataInspection->listTemplate((object) ['resellerID' => $calcObj['resellerID'],'action' => 'objectByID','objectID' => $productSQL[1]['ii_object_id'][$iIDKey]]);
$type = "text";
// Push values into arrays
array_push($idArr, $productSQL[1]['obj_ID'][$iIDKey]);
array_push($textArr, $objData[0]['Value'][0]);
array_push($contentType, $type);
array_push($parentIDArr, $productSQL[1]['obj_parentID'][$iIDKey]);
}
else
{
// Type Table
$type = "table";
}
}
$jsonHolder['id'] = $idArr;
$jsonHolder['text'] = $textArr;
$jsonHolder['parentID'] = $parentIDArr;
$jsonHolder['type'] = $contentType;
$test = $functions->buildTree($jsonHolder);
print_r($test);
}
else
{
// No data present
$json = "empty";
}
Save them into arrays and the save them into single array again and then putting it further to buldTree function that I tried to implement from Veerendra but without succsess PHP - How to build tree structure list?
Function
// Got this function from Stackoverflow :), thanks Veerendra :=
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach($elements['id'] as $elementsKey => $elementValues)
{
if ($elements['parentID'][$elementsKey] == $parentId)
{
$jsonArr['id'] = $elementValues;
$jsonArr['parentID'] = $elements['parentID'][$elementsKey];
$jsonArr['text'] = $elements['text'][$elementsKey];
$jsonArr['type'] = $elements['type'][$elementsKey];
$children = $this->buildTree($elements, $elementValues);
if ($children)
{
$elements['children'] = $jsonArr;
}
$branch[] = $jsonArr;
}
}
return $branch;
}
This is the result so far from $branch
Array
(
[0] => Array
(
[id] => 219
[parentID] => 0
[text] => butik
[type] => text
)
[1] => Array
(
[id] => 244
[parentID] => 0
[text] => kontor
[type] => text
)
)
Hope this question makes any sence?, I can explain further afcorse, just ask. Thanks