[英]how to write the condition?
$tree = taxonomy_get_tree($vid);
print "<li>"; // 1 line
foreach ($tree as $term ) { // 2 lines
$diffdepth=0;
if ($term->depth > $depth) {
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
上面是原始代码,现在我要根据$term->depth > $depth
设置条件输出。( print "<li>";
)这行。
即
if ($term->depth > $depth) {
echo '<li class="parent">';
}
else {
print "<li>";
}
但是$term->depth
可以在foreach循环后使用,但是我想在1行使用它,我该怎么办?
无需在线print
,而是将所需的输出分配给变量,然后在逻辑完成后将其发送到浏览器。
$tree = taxonomy_get_tree($vid);
$parent = ""; // 1 line
$child = "";
foreach ($tree as $term) { // 2 line
$diffdepth=0;
if ($term->depth > $depth) {
$parent = "<li class='parent'>";
$child .= "<ul class='haschild'><li>";
$depth = $term->depth;
} else {
$parent = "<li>";
}
}
echo $parent . $child;
请注意,您需要通过添加所有适用的</li>
和诸如此类的方法来完成此操作,但这应该可以帮助您入门。
使用计数器:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
else { echo '<li>'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
...
$counter++;
}
如果您需要根据自己的状况写更多的差异,可以将上面的文字变成:
$tree = taxonomy_get_tree($vid);
$counter = 0;
foreach ($tree as $term)
{
...
if ($term->depth > $depth)
{
if ($counter == 0) { echo '<li class="parent">'; }
print "<ul class='haschild'><li>";
$depth = $term->depth;
}
else
{
if ($counter == 0) { echo '<li>'; }
...
}
...
$counter++;
}
如果您想构建类似于父子层次结构的东西,则应通过单击此处进行检查
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.