mysql - PHP : can't make a comments tree -


i'm using php5 , mysql 5.6

my task make comments tree , give json. each comment has id , id of comment belongs, named rootid. comments db ordering datetime.

the solution made working in descending order. in ascending order tree form incorrect. , thing need show comments in ascending order.

if faced such before, please me solve problem

here's code:

// $result - associative array comments  ($i = 0; $i < $arraylength; ++$i){     if ($result[$i]['rootid'] != 0){         ($j = 0; $j < $arraylength; ++$j){             if ($result[$j]['id'] == $result[$i]['rootid']){                 if ($result[$j]['child'] != 0){ // case when there's @ least 1 child                     $result[$j]['child'][] = $result[$i];                 }                 else {                 $result[$j]['child'] = $result[$i]; // case when no children                 }                 break;             }         }         unset($result[$i]);     } } 

this little example of get.

[   { "id": "652061", "rootid": "0", "date_add": "2017-03-18 22:40:31", "child": [   {     "id": "652063",     "rootid": "652061",     "date_add": "2017-03-18 22:58:54",     "child": [       {         "id": "652072",         "rootid": "652063",         "date_add": "2017-03-18 23:13:54",         "child": [           {             "id": "652118",             "rootid": "652072",             "date_add": "2017-03-19 08:03:36",             "child": []           }         ]       }     ]   } ]   },   { "id": "651999", "rootid": "0", "date_add": "2017-03-18 19:26:10", "child": [   {     "id": "652104",     "rootid": "651999",     "date_add": "2017-03-19 01:17:32",     "child": []   },   {     "id": "652066",     "rootid": "651999",     "date_add": "2017-03-18 23:02:26",     "child": []   } ] 

} ]

but if change sql request

order datetime asc 

the tree forms in incorrect way, few comments finds parents, not all

solved issue. if someone'll face such problem - here's solution. know there ways less amount of lines of code, here want show algorithm working. works observably slow, maybe because of lots of cycles.

    $comments = array(         array(             'id' => '1',             'rootid' => '0',             'child' => array(),         ),         array(             'id' => '2',             'rootid' => '1',             'child' => array(),         ),         array(             'id' => '3',             'rootid' => '2',             'child' => array(),         ),         array(             'id' => '4',             'rootid' => '0',             'child' => array(),         ),         array(             'id' => '5',             'rootid' => '4',             'child' => array(),         ),         array(             'id' => '6',             'rootid' => '4',             'child' => array(),         ),         array(             'id' => '7',             'rootid' => '3',             'child' => array(),         ),array(             'id' => '8',             'rootid' => '3',             'child' => array(),         ),array(             'id' => '9',             'rootid' => '7',             'child' => array(),         ),array(             'id' => '10',             'rootid' => '7',             'child' => array(),         ),     );      $arraylength = sizeof($comments);      function godeeper(&$array, $comm, $arraylength){         ($i = 0; $i < $arraylength; ++$i){             if ($array[$i]['id'] == $comm['rootid']){                 if ($array[$i]['child'] != 0 ){                     $array[$i]['child'][] = $comm;                 }                 else {                     $array[$i]['child'] = $comm;                 }                 return true;             }             else {                 if ($array[$i]['child'] != 0){                     $success = godeeper($array[$i]['child'], $comm, $arraylength);                     if ($success == true) {                         return true;                     }                 }             }         }     }       ($i = 0; $i < $arraylength; ++$i) {         if ($comments[$i]['rootid'] != 0) {             ($j = 0; $j < $arraylength; ++$j) {                 if ($comments[$j]['child'] != 0) {                     if ($comments[$j]['id'] == $comments[$i]['rootid']) {                         $comments[$j]['child'][] = $comments[$i];                         unset($comments[$i]);                         break;                     } else {                         $success = godeeper($comments[$j]['child'], $comments[$i], $arraylength);                         if ($success == true) {                             unset($comments[$i]);                             break;                         }                     }                 }                 else {                     if ($comments[$j]['id'] == $comments[$i]['rootid']){                         $comments[$j]['child'] = $comments[$i];                         unset($comments[$i]);                         break;                     }                 }             }         }     } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -