排序类别失败和错误排序时填充
Padding when sorting categories fails and mis-sorts
我正在尝试为我的 Teamspeak 3 服务器(它有父子频道 channels/categories,以及子频道到子频道等等)编写一个频道查看器,但是在尝试设置它的样式和添加填充取决于它在哪里,它失败了,结果是这样的:
虽然它应该看起来像这样:(显然不是样式,但你明白我的意思)
这是我的代码:
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);
}
非常感谢任何帮助,谢谢!
阅读代码,您只是在不断地添加填充。 $padding++
将递增该值并将其保存回 $padding
。在下一个循环中,您会再次添加它。如果我不得不猜测,您可能希望它只是 $padding+1
,它会加 1,但不会用新的 $padding+1
值覆盖 $padding
。
我正在尝试为我的 Teamspeak 3 服务器(它有父子频道 channels/categories,以及子频道到子频道等等)编写一个频道查看器,但是在尝试设置它的样式和添加填充取决于它在哪里,它失败了,结果是这样的:
虽然它应该看起来像这样:(显然不是样式,但你明白我的意思)
这是我的代码:
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);
}
非常感谢任何帮助,谢谢!
阅读代码,您只是在不断地添加填充。 $padding++
将递增该值并将其保存回 $padding
。在下一个循环中,您会再次添加它。如果我不得不猜测,您可能希望它只是 $padding+1
,它会加 1,但不会用新的 $padding+1
值覆盖 $padding
。