排序类别时的填充失败并错误排序

时间:2016-07-15 21:28:35

标签: php css parent-child teamspeak

我正在尝试为我的Teamspeak 3服务器编码频道查看器(其中包含父频道和子频道/类别,以及子频道的子频道等等),但在尝试设置样式并添加填充时取决于在哪里,它失败了,结果如下:

how it looks

虽然看起来应该是这样的:(显然没有风格,但你明白了我的意思)

how it's supposed to look

这是我的代码:

private $_allChannels   = array();
private $_allClients    = array();

private function showChannels($parentID, $padding)
{
    $response   = '';

    foreach ($this->_allChannels as $channel) {
        $channelParent  = $channel['pid'];
        $channelID      = $channel['cid'];
        $channelName    = $channel['name'];

        if ($channelParent == $parentID) {
            $response   .= '<span style="margin-left: ' . $padding*2 . 'em;">' . $channelName . '</span><br>';
            $response   .= $this->showChannels($channelID, $padding++);
        }
    }

    return $response;
}

public function index()
{
    $teamspeakServer    = TeamSpeak3::factory("serverquery://user:pass@IP:QueryPort/?server_port=ServerPort");

    $allClients         = $teamspeakServer->clientList(['client_type' => 0]);
    $allChannels        = $teamspeakServer->channelList();

    foreach ($allChannels as $channel) {
        array_push($this->_allChannels, array('pid' => $channel['pid'], 'name' => $channel['channel_name'], 'cid' => $channel['cid']));
    }

    echo $this->showChannels(0, 0);
}

我会感激任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

阅读代码,您只是不断添加到填充。 $padding++会增加该值并将其保存回$padding。在下一个循环中,您将继续添加它。如果我不得不猜测,您希望只有$padding+1才能添加1,但不会使用新的$padding值覆盖$padding+1