This is my first time on Stackoverflow so please bear with me if my question is not clear !. I am trying to fetch all rows from tblproperty and loop through the array to convert it into an associative array. Once the property is fetched i am checking if the property is of the page or object. If the property if of the page we add the same in the propertiesArray. If the property is of object I am checking if the object is of the page or child of another parent object. The code below works for 3 levels (meaning Parent > Child > Child > Child). However I need to improve it to loop continuously if the object is of another parent object. Need advice how can I improve it. Further below code is in PHP on Server Side. I am looking to take it to Client Side. Any way to do it in Angular JS. Thanks in advance to all who chose to help !
$DBObj->querySelect("*","tblproperty","pgnid = $pageNID and propisactive = 1");
if($DBObj->fetchNumOfRows() >= 1){
$queryRecords = $DBObj->fetchAllRecordsArray();
$rows = $DBObj->fetchNumOfRows();
for($i = 0; $i < $rows; $i ){
//Properties of Objects are Loaded to Object Properties Array
switch ($queryRecords[$i]['propof']){
case "page":{
if(array_key_exists($queryRecords[$i]['propofid'],$propertiesArray) != 1)
$propertiesArray[$queryRecords[$i]['propofid']] = [];
array_push($propertiesArray[$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
case "object":{
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecords[$i]['propofnid']);
$queryRecord = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord != "")
switch($queryRecord['objof']){
case "object":{
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord['objofnid']);
$queryRecord1 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord1 != "")
switch($queryRecord1['objof']){
case "object":{
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord1['objofnid']);
$queryRecord2 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord2 != "")
switch($queryRecord2['objof']){
case "object":{
if(array_key_exists($queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
array_push($propertiesArray[$queryRecord2['objofid'].$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
case "page":{
if(array_key_exists($queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
array_push($propertiesArray[$queryRecord1['objofid'].$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
break;
}
case "page":{
if(array_key_exists($queryRecord['objofid'].$queryRecords[$i]['propofid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord['objofid'].$queryRecords[$i]['propofid']] = [];
array_push($propertiesArray[$queryRecord['objofid'].$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
break;
}
case "page":{
if(array_key_exists($queryRecords[$i]['propofid'],$propertiesArray) != 1)
$propertiesArray[$queryRecords[$i]['propofid']] = [];
array_push($propertiesArray[$queryRecords[$i]['propofid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
break;
}
case "element":{
$DBObj->querySelect("*","tblelement","pgnid = $pageNID and elisactive = 1 and elnid = ".$queryRecords[$i]['propofnid']);
$queryRecord = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord != ""){
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord['elofnid']);
$queryRecord1 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord1 != "")
switch($queryRecord1['objof']){
case "object":{
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord1['objofnid']);
$queryRecord2 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord2 != ""){
switch($queryRecord2['objof']){
case "object":{
$DBObj->querySelect("*","tblobject","pgnid = $pageNID and objisactive = 1 and objnid = ".$queryRecord2['objofnid']);
$queryRecord3 = ($DBObj->fetchNumOfRows() == 1)?$DBObj->fetchRecordArray():"";
if($queryRecord3 != ""){
switch($queryRecord3['objof']){
case "object":{
break;
}
case "page":{
if(array_key_exists($queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']] = [];
array_push($propertiesArray[$queryRecord3['objid'].$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
}
break;
}
case "page":{
if(array_key_exists($queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']] = [];
array_push($propertiesArray[$queryRecord2['objid'].$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
}
break;
}
case "page":{
if(array_key_exists($queryRecord1['objid'].$queryRecord['elid'],$propertiesArray) != 1)
$propertiesArray[$queryRecord1['objid'].$queryRecord['elid']] = [];
array_push($propertiesArray[$queryRecord1['objid'].$queryRecord['elid']],array($queryRecords[$i]['propid'] => $queryRecords[$i]['propvalue'],'proplinkedto' => $queryRecords[$i]['proplinkedto']));
break;
}
}
}
break;
}
}
}
}
CodePudding user response:
To iterate your object recursively you need ... recursion
Create separate function with conditions inside.
- If current item belongs to page - push to array and return
- If current item belongs to object - take child element and repeat this check again.
This is pseudo code, adjust how you need it:
if($DBObj->fetchNumOfRows() >= 1){
$queryRecords = $DBObj->fetchAllRecordsArray();
$rows = $DBObj->fetchNumOfRows();
for($i = 0; $i < $rows; $i ){
recursiveAction($queryRecords[$i]);
}
}
function recursiveAction(&$item, $propertiesArray) {
switch ($item['propof']) {
case 'page':
// No need for that "if" check
$propertiesArray[$item['propofid']][] = [$item['propid'] => $item['propvalue'],'proplinkedto' => $item['proplinkedto']];
break;
case 'object':
$DBObj->querySelect(...);
$queryRecord = $DBObj->fetchNumOfRows() == 1 ? $DBObj->fetchRecordArray() : "";
if (!empty($queryRecord)) {
// Here we convert common part of "object" into recursion
recursiveAction($queryRecord);
}
break;
case 'element':
...
}
}