显示分层数据
我正在玩一个我在这里找到的关于“树形菜单”的代码示例,并想提出这个问题。
function tree($id)
{
$query = "SELECT `name`,`id` from `table` WHERE `id_parrent` = '$id'";
$result = mysql_query($query);
if(mysql_num_rows($result) != 0)
{
echo "<ul>";
while($row = mysql_fetch_array($result))
{
echo "<li>",$row[name],"</li>";
tree($row[id]);
}
echo "</ul>";
}
}
如果我想以这样的方式显示项目怎么办:
<ul>
<li>item 1</li>
<li>item 2</li>
<li style="padding-left:10px;">item 3-has parent 2</li>
<li style="padding-left:20px;">item 4-has parent 3</li>
<li style="padding-left:10px;">item 5-has parent 2</li>
<li>item 6</li>
</ul>
我的主要问题是以某种方式找到级别,以便我可以乘以 level*padding 并创建我的列表。 任何建议将不胜感激。
I am playing around with a code example i found here about 'tree menu' and wanted to make this question.
function tree($id)
{
$query = "SELECT `name`,`id` from `table` WHERE `id_parrent` = '$id'";
$result = mysql_query($query);
if(mysql_num_rows($result) != 0)
{
echo "<ul>";
while($row = mysql_fetch_array($result))
{
echo "<li>",$row[name],"</li>";
tree($row[id]);
}
echo "</ul>";
}
}
what if i want to display items in a way like this:
<ul>
<li>item 1</li>
<li>item 2</li>
<li style="padding-left:10px;">item 3-has parent 2</li>
<li style="padding-left:20px;">item 4-has parent 3</li>
<li style="padding-left:10px;">item 5-has parent 2</li>
<li>item 6</li>
</ul>
My main problem is to find the level somehow so that i could multiply level*padding and create my list.
Any suggestions would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要
tree
函数来跟踪级别,然后在级别大于 1 时应用填充。You need your
tree
function to track the level, and then apply padding if the level is greater than 1.我有一家商店,其递归库存类别与您正在做的类似。这是我编写的代码,将其呈现为无序列表(对此示例稍作修改)。希望有帮助!
I have a store with recursive inventory categories similar to what you're doing. Here's the code I wrote to present it as an unordered list (slightly modified for this example). Hope it helps!