[英]how do i reference a dynamically created list php
I have the following code to create category and subcategories from a database 我有以下代码从数据库创建类别和子类别
function displayChild($parent)
{
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc())
{
echo '<li><a href="#">'.mb_strtoupper($rows['catName']).'</a></li>';
displayChild($rows['catId']);
}
echo '</ul></div>';
}
displayChild('0');
The CSS bits should be as follows CSS位应如下
@charset "utf-8";
.ul0{}
.ul1{}
.ul2{}
.
.
.
.uln{}
since the first tag appears outside the while loop it forms a really weird list when i reference it.putting it inside the while() loop is not an option either. 因为第一个标记出现在while循环之外,所以当我引用它时会形成一个很奇怪的列表。把它放在while()循环中也不是一种选择。 please help
请帮忙
I'm not exactly sure what you mean by " it forms a really weird list when i reference it." 我不确定您的意思是“当我引用它时,它形成了一个非常奇怪的列表”。 However, it sounds like maybe you are getting odd html.
但是,听起来您可能会收到奇怪的html。 If that's the case, you get better results if you put the nested function call inside the li:
在这种情况下,如果将嵌套函数调用放在li内,则会得到更好的结果:
function displayChild($parent){
include 'connect.php';
$parentid=$parent;
$catSql='select* from categories where parentid='.$parentid.'';
$result=$con->query($catSql);
echo '<div><ul class='.'ul'.$parentid.'>';
while ($rows=$result->fetch_assoc()){
echo '<li><a href="#">'.mb_strtoupper($rows['catName']).'</a>';
displayChild($rows['catId']); // note this is now inside the list item
echo '</li>';
}
echo '</ul></div>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.