A
A
Anton2021-08-25 08:06:06
PHP
Anton, 2021-08-25 08:06:06

How to display subcategories of a category?

Hello.
I'm trying to display its subcategories from the category, that is, I don't want to display the category itself, but only the subcategories of this category, let's say I know the id 21108 of the category from which I want to display its subcategories.

At the moment, what happened to me is that I catch the category by id that I display, but I don’t need to display it, I want to display subcategories from it.

<?php foreach ($categories as $category) {
        if ($category['id'] == '21108') {
      ?>
        <li>
          <a href="<?php echo $category['alias'] ?>/" title="<?= $category['name'] ?>"><?= $category['name'] ?></a>
        </li>
      <?php
      }
      }
      ?>


I have a helper that outputs everything, but I don't need everything.
I still can't figure out how to implement what I want.
I throw off the helper code so that there is a general understanding of the output of categories and subcategories.

public static function viewCatUnlimited($categories, $level = 0, $countLevel = null)
  {
    // через запятую 1,2 и так далее, указываем в каком уровне выводить стрелку.
    $array_parent = array(1);
    $array_parents = array(2, 3);
    // через запятую 1,2 и так далее, указываем где выводить всплывающие подменю.
    $array_container = array(1);

    //выводим в ul класс для 2 уровня
    $second_menu = array(2);
    //выводим в ul класс для 3,4 и так далее уровня
    $third_menu = array(3, 4, 5, 6, 7);

    //$array_haspopup = array(1);

    foreach ($categories as $category) {
      if ($category['show_cat'] == '1') {
        if (!isset($category['children'])) {
          $parentCat = ORM::factory('Category', $category['id']);
?>
          <li><a href="<?= self::getUrlParentAlias($category['id']) ?>" title=""><?= (in_array($level, $array_parent)) ? '<i class="' . $category['icon'] . '"></i>' : '' ?><?= $category['name'] ?></a></li>
        <?php
        } else {
        ?>

          <li>
            <?php
            if ($level != 0) {
              $parentCat = ORM::factory('Category', $category['parent_id']);
            ?>
              <a href="<?= self::getUrlParentAlias($category['id']) ?>" title=""><?= (in_array($level, $array_parent)) ? '<i class="' . $category['icon'] . '"></i>' : '' ?><?= $category['name'] ?>
                <?= (in_array($level, $array_parent)) ? '<i class="icon-smgmr"></i><i class="icon-smgmr1" onclick="MenuClick(this);return false;"></i>' : '' ?><?= (in_array($level, $array_parents)) ? '<i class="icon-smgmr2" onclick="MenuClick(this);return false;"></i>' : '' ?></a>
            <?php
            }
            ?>
            <?php
            $showChild = false;
            foreach ($category["children"] as $childfirst) {
              if ($childfirst['show_cat'] == '1') {
                $showChild = true;
              }
              break;
            }

            if ($showChild) {
              if ($countLevel !== null && $level >= $countLevel) {
              } else {

                if (in_array($level, $array_container)) {
            ?>
                  <div class="podm allpod" onmouseover="HoverElem(this);" onmouseout="StopHover(this);">
                  <?php
                }
                  ?>
                  <ul <?= (in_array($level, $array_parent)) ? 'class="podm1"' : '' ?> <?= (in_array($level, $second_menu)) ? 'class="second_menu"' : '' ?> <?= (in_array($level, $third_menu)) ? 'class="third_menu"' : '' ?>>
                    <?php
                    self::viewCatUnlimited($category["children"], $level + 1, $countLevel);
                    ?>
                  </ul>
                  <?php
                  if (in_array($level, $array_container)) {
                  ?>
                  </div>
              <?php
                  }
                }
              ?>
          </li>
<?php
            }
          }
        }
      }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2021-08-26
Websaytovsky @ws17

So well, it seems like even I managed to do it myself, output, there is no error, although I am zero in php)

<?php
              foreach ($categories as $category) :
                if ($category['id'] == '21108') :
                  if (!isset($category['children'])) {
              ?>
                  <?
                  } 
                  ?>
                    <!--   вывод уровень 2 -->
                      <?php
                      foreach ($category["children"] as $childfirst) :
                        if ($childfirst['show_cat'] == '1') {
                          if (!isset($childfirst['children'])) {
                      ?>
                            <li><a href="/uborka-zdaniy/<?= $childfirst['alias'] ?>/" title="<?= $childfirst['name'] ?>"><?= $childfirst['name'] ?></a></li>
                            <?
                          }
                        }
                      endforeach;
                      ?>
              <?
                endif;
              endforeach;
              ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question