2017-04-06 59 views
0

我正在使用php5和mysql 5.6PHP:無法發表評論樹

我的任務是創建一個評論樹並將其全部視爲json。每個評論都有其所屬的評論的ID和ID,名爲rootId。 我從日期時間的數據庫中獲得了評論。

我所做的解決方案僅以降序工作。按升序排列樹形不正確。而事情是我需要按照升序顯示評論。

如果有人這樣之前遇到,請幫我解決這個問題

這裏是我的代碼:

// $result - associative array with all comments 

for ($i = 0; $i < $arrayLength; ++$i){ 
    if ($result[$i]['rootid'] != 0){ 
     for ($j = 0; $j < $arrayLength; ++$j){ 
      if ($result[$j]['id'] == $result[$i]['rootid']){ 
       if ($result[$j]['child'] != 0){ // the case when there's at least 1 child 
        $result[$j]['child'][] = $result[$i]; 
       } 
       else { 
       $result[$j]['child'] = $result[$i]; // case when no children 
       } 
       break; 
      } 
     } 
     unset($result[$i]); 
    } 
} 

這是什麼,我得到一個小例子。

[ 
    { 
"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": [] 
    } 
] 

} ]

但如果我更改SQL請求

ORDER BY datetime ASC 

不正確的方式樹的形式,只有少數意見認爲他們的父母,不是所有的

+0

什麼意思是「降序」和「升序」的順序?在你的具體情況下,它會更好地工作,首先由rootId命令,然後是時間戳? –

+0

如果您提供結果數組的小示例,當前輸出和預期輸出,它會有所幫助。 – k0pernikus

+0

請閱讀:https://stackoverflow.com/help/mcve – k0pernikus

回答

0

解決問題。如果有人會遇到這樣的問題 - 這就是解決方案。我知道有很多方法可以用少量的代碼來完成,但是在這裏我想展示算法的工作。它的工作速度也很慢,可能是因爲很多週期。

$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){ 
     for ($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; 
        } 
       } 
      } 
     } 
    } 


    for ($i = 0; $i < $arrayLength; ++$i) { 
     if ($comments[$i]['rootid'] != 0) { 
      for ($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; 
        } 
       } 
      } 
     } 
    }