A
A
Anton2016-06-15 12:20:18
Kohana
Anton, 2016-06-15 12:20:18

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

2 answer(s)
N
Nikolai Shvetsov, 2016-06-15
@ShNn

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?

E
entermix, 2016-06-15
@entermix

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 question

Ask a Question

731 491 924 answers to any question