dongyinzhi4689 2016-07-15 21:28
浏览 21
已采纳

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

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!

  • 写回答

1条回答 默认 最新

  • duanbinren8906 2016-07-15 21:46
    关注

    Reading over the code, you are just constantly adding to the padding. $padding++ will increment the value and save it back into $padding. On next loop you keep adding to it again. If I had to guess, you would want that to be just $padding+1 which would add 1, but not overwrite $padding with the new $padding+1 value.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料