从fetch_assoc()构建一个评论树(表单mysqli用getresult准备)

时间:2014-01-21 11:07:02

标签: php tree

我上课了..

$this->article_id = 14;
$this->res = $this->conn->prepare("Select id,name, parent_id FROM Comments WHERE article_id = ? ORDER BY id");
$this->res->bind_param('i', $this->article_id);
$this->res->execute();
$this->res = $this->res->get_result();

使用..

echo '<ul>';
while($row = $this->res->fetch_assoc()) {
  echo "<li>comment $row[name]-id:$row[id] parent: $row[parent_id]</li>";
}
echo '</ul>';
// Result:
<ul>
  <li>comment foo1-id:1 parent: 0</li>
  <li>comment foo2-id:2 parent: 4</li>
  <li>comment foo3-id:3 parent: 0</li>
  <li>comment foo4-id:4 parent: 1</li>
  <li>comment foo5-id:5 parent: 2</li>
  <li>comment foo6-id:6 parent: 2</li>
  <li>comment foo7-id:7 parent: 5</li>
  <li>comment foo8-id:8 parent: 1</li>
  <li>comment foo9-id:9 parent: 3</li>
  <li>comment foo10-id:10 parent: 1</li>
</ul>

现在我想要一个以树模式显示注释的结果,以突出显示注释及其回复...:

<ul>
  <li>comment foo1-id: 1 parent: 0</li>
  <li>
    <ul>
      <li>comment foo4-id: 4 parent: 1</li>
      <li>  
        <ul>
          <li>comment foo2-id:2 parent: 4</li>
          <li>
            <ul>
              <li>comment foo5-id:5 parent: 2</li>
              <li>
                <ul>
                  <li>comment foo7-id:7 parent: 5</li>
                </ul>
              </li>
              <li>comment foo6-id:6 parent: 2</li>
            </ul>
          </li>
        </ul>
      </li>
      <li>comment foo8-id: 8 parent: 1</li>
      <li>comment foo10-id: 10 parent: 1</li>
    </ul>
  </li>
  <li>comment foo3-id: 3 parent: 0</li>
  <li>
    <ul>
      <li>comment foo9-id:9 parent: 3</li>
    </ul>
  </li>      
</ul>

我尝试了很多解决方案,但工作不正常...... Whit递归函数,例如:

$comments = array();
while($row = $this->res->fetch_assoc()) {
  $row['children'] = array();
  $comments[$row['id']] = $row;
}
foreach ($comments as $k => &$v) {
  if ($v['parent_id'] != 0) {
    $comments[$v['parent_id']]['children'][] =& $v;
  }
}
unset($v);

// delete the childs comments from the top level
foreach ($comments as $k => $v) {
  if ($v['parent_id'] != 0) {
    unset($comments[$k]);
  }
}

function toUl ($arr) {
    $html = '<ul>' . PHP_EOL;
    foreach ( $arr as $v ) {
        $html.= '<li>comment '.$v['name'].'-id'.$v['id'].' parent: '.$v['parent_id'].'</li>' . PHP_EOL;
        if ( array_key_exists('children', $v) ) {
            $html.= toUL($v['children']);
        }
    }
    $html.= '</ul>' . PHP_EOL;
    return $html;
}

echo toUl($comments);

var_dump($comments):
array(2) {
  [3]=> array(4) {
    ["id"]=> int(3)
    ["parent_id"]=> int(0)
    ["name"]=> string(4) "foo3"
    ["children"]=> array(0) { }
  }
  [1]=> array(4) {
    ["id"]=> int(1)
    ["parent_id"]=> int(0)
    ["name"]=> string(4) "foo1"
    ["children"]=> array(1) {
      [0]=> array(4) {
        ["id"]=> int(4)
        ["parent_id"]=> int(1)
        ["name"]=> string(4) "foo4"
        ["children"]=> array(1) {
          [0]=> array(4) {
            ["id"]=> int(2)
            ["parent_id"]=> int(4)
            ["name"]=> string(4) "foo2"
            ["children"]=> array(0) { }
          }
        }
      }
    }
  }
}
array(2) {
  [3]=> array(4) {
    ["id"]=> int(3)
    ["parent_id"]=> int(0)
    ["name"]=> string(4) "foo3"
    ["children"]=> array(0) { }
  }
  [1]=> array(4) {
    ["id"]=> int(1)
    ["parent_id"]=> int(0)
    ["name"]=> string(4) "foo1"
    ["children"]=> array(1) {
      [0]=> array(4) {
        ["id"]=> int(4)
        ["parent_id"]=> int(1)
        ["name"]=> string(4) "foo4"
        ["children"]=> array(1) {
          [0]=> array(4) {
            ["id"]=> int(2)
            ["parent_id"]=> int(4)
            ["name"]=> string(4) "foo2"
            ["children"]=> array(0) { }
          }
        }
      }
    }
  }
}

输出:

<ul>
  <li>comment foo3-id3 parent: 0</li>
  <ul>
  </ul>
  <li>comment foo1-id1 parent: 0</li>
  <ul>
    <li>comment foo4-id4 parent: 1</li>
    <ul>
      <li>comment foo2-id2 parent: 4</li>
      <ul>
      </ul>
    </ul>
  </ul>
</ul>

出了点问题......: - (

1 个答案:

答案 0 :(得分:0)

不是直接在fetch_assoc循环中回显结果,而是尝试在其中定义一个多维数组,然后循环遍历该数组以回显HTML结构。