我有一个关于在php循环之间放置正确的div持有者的问题。 我正在创建一个包含类别和子主题的菜单,如in this image所示。
类别可以包含子类别,但并非总是如此。我已经完成了这样做的PHP代码。 但是我的php循环和foreach循环中的标签放置有一些问题。
这是应该如何显示的HTML代码:
<!--Category Menu -->
<div class='categoriesmenuFather ' id='nav-cat' style='display:block' >
<div class='categories_list categoriesmenu '>
<ul class='level1'>
<li class='submenu border_t'>
<div class=cat_bg>
<a title='Electronics' href='#'><span>Electronics</SPAN></a>
</div>
<!--BOF Sub-category-->
<ul class='level2'>
<li><a href="#">LED</a></li>
<li><a href="#">DVD Players</a></li>
</ul>
</li>
<!--EOF Sub-category-->
<li class='submenu border_t'>
<div class=cat_bg>
<a title='Electronics' href='#'><span>Computers & Networking</SPAN></a>
</div>
<!--BOF Sub-category-->
<ul class='level2'>
<li><a href="#">Tablet</a></li>
</ul>
</li>
<!--EOF Sub-category-->
<li class='submenu border_t'>
<div class=cat_bg>
<a title='Electronics' href='#'><span>Cell Phones</SPAN></a>
</div>
</ul>
</div>
</div>
使用我现在的循环代码,html显示为:
<!--BOF Left Menu -->
<div class='categoriesmenuFather ' id='nav-cat' style='display:block' >
<div class='categories_list categoriesmenu '>
<ul class='level1'>
<li class='submenu border_t'>
<div class=cat_bg>
<a title='Electronics' href='products.php?catid=1'><span>Electronics</SPAN></a>
</div>
<!--BOF Sub-category-->
<ul class='level2'>
<li><a href="products.php?catid=1&subcatid=1">LED</a></li>
<li><a href="products.php?catid=1&subcatid=2">DVD Players</a></li>
</ul></li>
<!--EOF Sub-category-->
<li class='submenu border_t'>
<div class=cat_bg>
<a title='Computers' href='products.php?catid=2'><span>Computers & Networking</SPAN></a>
</div>
<!--BOF Sub-category-->
<ul class='level2'>
<li><a href="products.php?catid=2&subcatid=3">Tablet</a></li>
</ul></li>
<!--EOF Sub-category-->
<li class='submenu border_t'>
<div class=catall_bg>
<a title='Cell Phones' href='products.php?catid=3'><span>Cell Phones</SPAN></a>
</div>
</ul></li>
<!--EOF Sub-category-->
<li class='submenu border_t'>
<div class=catall_bg>
<a title='Video Games' href='products.php?catid=4'><span>Video Games</SPAN></a>
</div>
</ul></li>
</ul>
</div>
</div>
在没有像手机和视频游戏这样的子类别的产品中/ ul和/ li
标记将包含在最后一项显示为in this image (design changed)的结果中。
我的php代码如下:
<?php
include_once 'connstring.inc.php';
//Get records from database
$q = $conn->prepare("SELECT DISTINCT *, C.category_id
FROM category C LEFT OUTER JOIN subcategory S
ON C.category_id = S.category_id AND S.status = 'Enabled'
WHERE C.status = 'Enabled'
");
$q->execute();
$result = $q->fetchAll(PDO::FETCH_ASSOC|PDO::FETCH_GROUP);
if ($result !== false) {
?>
<div class='categoriesmenuFather ' id='nav-cat' style='display:block' >
<div class='categories_list categoriesmenu '>
<ul class='level1'>
<?php
foreach($result as $key => $rows) {
if (!empty($rows)) {
$i = 1;
foreach($rows as $row) {
if ($i == 1) {
?>
<li class='submenu border_t'>
<?php
//to not display arrow, check for subcategory items is none
if (is_null($row['subcategory_name']) || (string)$row['subcategory_name'] == ''){
?>
<div class=catall_bg>
<a title='<?php echo $row['category_name']; ?>' href='products.php?catid=<?php echo $row['category_id']; ?>'><span><?php echo $row['category_name']; ?></SPAN></a>
</div>
<?php
}
else
{
?>
<div class=cat_bg> <!--for sub-cat-arrow-->
<a title='<?php echo $row['category_name']; ?>' href='products.php?catid=<?php echo $row['category_id']; ?>'><span><?php echo $row['category_name']; ?></SPAN></a>
</div><!--sub-cat-arrow close-->
<?php
}
?>
<?php
//to not display subcategory items if none
if (is_null($row['subcategory_name']) || (string)$row['subcategory_name'] == ''){
break;
}
?>
<!--BOF Sub-category-->
<ul class='level2'>
<?php
}
?>
<li><a href="products.php?catid=<?php echo $row['category_id'].'&subcatid='.$row['subcategory_id']; ?>"><?php echo $row['subcategory_name']; ?></a></li>
<?php
$i++;
}
echo'</ul></li>';
?>
<!--EOF Sub-category-->
<?php
}
}
} else {
throw new PDOException('No records');
}
?>
</ul>
</div>
</div>
我知道这是将标签放在循环中的问题..但我无法找到它。请求帮助。
答案 0 :(得分:0)
看看这部分:
<?php
//to not display subcategory items if none
if (is_null($row['subcategory_name']) || (string)$row['subcategory_name'] == ''){
break;
}
?>
<!--BOF Sub-category-->
<ul class='level2'>
<?php
}
?>
<li><a href="products.php?catid=<?php echo $row['category_id'].'&subcatid='.$row['subcategory_id']; ?>"><?php echo $row['subcategory_name']; ?></a></li>
<?php
$i++;
}
echo'</ul></li>';
?>
<!--EOF Sub-category-->
<?php
}
}
在这里你可以找到你的问题。您正在关闭因休息而未打开的ul标签;
我认为你需要关闭ul标签如果被打开,“破解”解决方案对我来说有点讨厌......更好的控制如果ul被打开,关闭ul,如果不仅需要关闭li。< / p>
答案 1 :(得分:0)
我认为您必须将<ul class='level2'>
放在if ($i == 1) {
目前它位于if
条件