mysql - Rewrite manually created nested JSON to use PHPs built in functions -


i have in table records related 1 parentid field. when fetching them db need create json array of objects child records added 'children' property of parent objects :

[{     //main object     children : [         {             //#1st level children object             children: [                 {                     //#2nd level children object                 }             ]         }     ] }, { (...) ] 

but if there no records parentid equal current record, property shouldn't added object.

right i'm building json string manually :

//first parents $q = 'select * table parentid = 0'; $r = mysql_query($q); $x=1; $nr = mysql_num_rows($r); while($e = mysql_fetch_array($r)){         if($x==1){                 echo '[ {       ';         }         else{                 echo ' { ';              }          if($e['leaf']==1){                 $leaf = 'true';          } else {                 $leaf = 'false';         }          echo '"enddate" : "'.str_replace('t00:00:00','',$e['enddate']).'",                   "id" : '.$e['id'].',                   "name" : "'.$e['name'].'",                   "baselinestartdate" : "'.str_replace('t00:00:00','',$e['baselinestartdate']).'"';         if($leaf){                 echo ',"leaf": '.$leaf.'';         }          childs($e['id'], $x, $nr);         if($x<$nr){                 echo ',';         }                $x++; } if($x>1){         echo ']'; }    function childs($id, $x, $nr, $x2='', $nr2=''){         //now see if there childern         $q2 = 'select * table parentid = "'.$id.'" ';         $r2 = mysql_query($q2);         $nr2 = mysql_num_rows($r2);         if($nr2>0){                 echo ',"children": [ ';                  $x2 =1;                 while($e2 = mysql_fetch_array($r2)){                          if($e2['leaf']==1){                                 $leaf2 = 'true';                                 }                         else{                                 $leaf2 = 'false';                         }                          echo '{                               "enddate" : "'.str_replace('t00:00:00','',$e2['enddate']).'",                                   "id" : '.$e2['id'].',                                   "name" : "'.$e2['name'].'",                                   "baselinestartdate" : "'.str_replace('t00:00:00','',$e2['baselinestartdate']).'",                                   "leaf" : "'.$leaf2.'"';                          childs($e2['id'],$x,$nr,'',$x2,$nr2);                         if($x2<$nr2){                                 echo ',';                                }                         $x2++;                  }                 echo '] }';         }         else{                 echo ',"children" : []}';                }             } 

is there easy way make code more robust using built-in php features fetch_assoc or that?

you should rather..

  1. fetch results database
  2. reformat per requirement (related code can use alteration php create multidimensional array array relational data
  3. then finally, json_encode resulting array

Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -