0

I'm trying to code a channel viewer for my Teamspeak 3 server (which has parent and child channels/categories, and child channels to the child channels and so on), but when trying to style it and add padding depends on where it is, it fails and turns out like this:

how it looks

While it's supposed to look like this: (obviously not as styled, but you get my point)

how it's supposed to look

Here's my code:

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);
}

I'll appreciate any help, thanks!

4

1 回答 1

1

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

于 2016-07-15T21:46:22.177 回答