Answer the question
In order to leave comments, you need to log in
Displaying subcategories from a category by id, how to do it right?
Hello, on the site in a certain place I want to display subcategories from one category, display only subcategories that are all included in the category.
My code, but it displays only the category itself, and does not display the subcategories that are included in it.
<?php foreach ($categories as $cat): ?>
<?php if($cat['id'] == 1300): ?>
<li><a href="/category/<?= $cat['alias']?>" title="<?= $cat['name'] ?>"><?= $cat['name'] ?></a></li>
<?php endif; ?>
<?php endforeach; ?>
Answer the question
In order to leave comments, you need to log in
1) Write the structure of the array. It’s not very clear where there are subcategories. 2) Where does this array come from, is it not from the database?
Well, in general, something like this (if you use Kohana ORM):
<ul>
<? foreach ($categories as $cat): ?>
<li><a href="/category/<?= $cat->alias; ?>" title="<?= $cat->name; ?>"><?= $cat->name; ?></a></li>
<? if ($cat->subcategories->count_all() > 0): ?>
<li>
<ul>
<? foreach ($cat->subcategories->find_all() as $subcategory): ?>
<li><?= $subcategory->name; ?></li>
<? endforeach; ?>
</ul>
</li>
<? endif; ?>
<? endforeach; ?>
</ul>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question