所以基本上我想在第一个foreachloop中的每个项目上显示第二个foreach循环项目,但现在它只显示在第一个foreach循环的第一个项目而不是foreach循环中的每个项目。
我的变数:
/* Queries */
$categories = $conn->query('SELECT pc.name, pc.ID, COUNT(p.id) AS amount FROM productcategory AS pc INNER JOIN products AS p ON p.categoryID = pc.ID WHERE p.available = 1 GROUP BY pc.ID');
$categories->execute();
$subcategories = $conn->query('SELECT pcs.name, pcs.categoryID, pcs.ID FROM productsubcategory AS pcs GROUP BY pcs.ID');
$subcategories->execute();
foreach ($categories as $category) {
if(isset($_GET['categorie'])) {
if($category["name"] === $_GET['categorie']) {
$class = 'class="selected"';
} else {
$class = '';
}
}
?>
<li><a href="?categorie=<?php echo $category["name"]; ?>" <?php echo $class; ?>> <?php echo $category["name"] . ' ('. $category["amount"] .')'; ?></a></li>
<?php
foreach ($subcategories as $subcategory) {
if($subcategory['categoryID'] == $category['ID']) {
?>
<li class="sub-cat"><a href="?categorie=<?php echo $subcategory["name"]; ?>" <?php echo $class; ?>> <?php echo $subcategory["name"]; ?></a></li>
<?php
}
}
}
输出
.sub-cat {
margin-left: 10px;
}
<ul>
<li><a href="?categorie=Koffie"> Koffie (2)</a></li>
<li class="sub-cat"><a href="?categorie=extra sterk"> extra sterk</a></li>
<li class="sub-cat"><a href="?categorie=Sterk"> Sterk</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Honing"> Honing</a></li>
<li class="sub-cat"><a href="?categorie=Honing"> Honing</a></li>
<li class="sub-cat"><a href="?categorie=Bon bons"> Bon bons</a></li>
<li class="sub-cat"><a href="?categorie=Puur"> Puur</a></li>
<li><a href="?categorie=Thee"> Thee (4)</a></li>
<li><a href="?categorie=Chocolade"> Chocolade (2)</a></li>
<li><a href="?categorie=Olie"> Olie (1)</a></li>
<li><a href="?categorie=Wijn"> Wijn (2)</a></li>
<li><a href="?categorie=Accessoires"> Accessoires (2)</a></li>
<li><a href="?categorie=Cadeaus"> Cadeaus (1)</a></li>
</ul>
预期产量: 所以基本上我想在正确的类别中显示好的子类别。我知道我需要使用if语句来比较子类别类别ID是否与类别ID相同,但第二个循环停止并且是第一个循环的第一个元素。
.sub-cat {
margin-left: 10px;
}
<ul>
<li><a href="?categorie=Koffie"> Koffie (2)</a></li>
<li class="sub-cat"><a href="?categorie=extrasterk"> extra sterk</a></li>
<li class="sub-cat"><a href="categorie=Sterk"> Sterk</a></li>
<li><a href="?categorie=Thee"> Thee (4)</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Zoete"> Zoete</a></li>
<li class="sub-cat"><a href="?categorie=Honing"> Honing</a></li>
<li class="sub-cat"><a href="?categorie=Honing"> Honing</a></li>
<li><a href="?categorie=Chocolade"> Chocolade (2)</a></li>
<li class="sub-cat"><a href="?categorie=Bon bons"> Bon bons</a></li>
<li class="sub-cat"><a href="?categorie=Puur"> Puur</a></li>
<li><a href="?categorie=Olie"> Olie (1)</a></li>
<li><a href="?categorie=Wijn"> Wijn (2)</a></li>
<li><a href="?categorie=Accessoires"> Accessoires (2)</a></li>
<li><a href="?categorie=Cadeaus"> Cadeaus (1)</a></li>
</ul>