A
A
arsenaljek2020-05-28 10:58:53
PHP
arsenaljek, 2020-05-28 10:58:53

Output Parent-Category-Subcategory-Product?

Tell me please. I've been scratching my head for a few days now.
There is an array (Parent-Category-Subcategory)

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Родитель 1
            [layer] => 1
            [parent_id] => 0
            [link] => 
        )

    [1] => Array
        (
            [id] => 10
            [name] => Категория 1
            [layer] => 2
            [parent_id] => 1
            [link] => урл_категории
        )

    [2] => Array
        (
            [id] => 178
            [name] => Подкатегория 1
            [layer] => 3
            [parent_id] => 10
            [link] => урл_подкатегории
        )

    [3] => Array
        (
            [id] => 56
            [name] => Подкатегория 2
            [layer] => 3
            [parent_id] => 10
            [link] => урл_подкатегории
        )

    [4] => Array
        (
            [id] => 57
            [name] => Подкатегория 3
            [layer] => 3
            [parent_id] => 10
            [link] => урл_подкатегории
        )

    [5] => Array
        (
            [id] => 192
            [name] => Категория 2
            [layer] => 2
            [parent_id] => 1
            [link] => урл_категории
        )

    [6] => Array
        (
            [id] => 194
            [name] => Подкатегория 3
            [layer] => 3
            [parent_id] => 192
            [link] => урл_подкатегории
        )

    [21] => Array
        (
            [id] => 2
            [name] => Родитель 2
            [layer] => 1
            [parent_id] => 0
            [link] => 
        )

    [22] => Array
        (
            [id] => 13
            [name] => Категория 1
            [layer] => 2
            [parent_id] => 2
            [link] => урл_категории
        )

    [23] => Array
        (
            [id] => 67
            [name] => Подкатегория 1
            [layer] => 3
            [parent_id] => 13
            [link] => урл_подкатегории
        )

    [24] => Array
        (
            [id] => 185
            [name] => Подкатегория 2
            [layer] => 3
            [parent_id] => 13
            [link] => урл_подкатегории
        )
)

I am trying to output in bootstrap accordion this whole system.
Here is the output code (This code also contains a level with a product). There's just a template 1 product for the test.
<div class="row">
            <div class="col-12">
              <div class="accordion" id="accordionExample">
                <?php
                $i = 1;
                foreach($left_menu as $menu_item) { 
                  if($menu_item['layer']==3) { ?>
                    <div class="card-body p-0 pt-1">
                      <h3 class="mb-0 text-center alert-primary" style="color:white"><?=$menu_item['name']?></h3>
                      <div class="table-responsive">
                        <table class="table table-sm mb-0">
                          <thead>
                            <tr>
                              <th scope="col"></th>
                              <th scope="col">Наименование</th>
                              <th scope="col">Описание</th>
                              <th scope="col">Цена завода (опт)</th>
                              <th scope="col">Цена базовая</th>
                              <th scope="col"></th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr>
                              <td><div class="badge badge-danger my-2">Хит продаж</div></td>
                              <td><a href="#">Товар 1</a></td>
                              <td class="w-75">Описание</td>
                              <td>8 190 <i class="fas fa-ruble-sign"></i></td>
                              <td>9 635 <i class="fas fa-ruble-sign"></i></td>
                              <td class="table-action"><a href="#"><i class="align-middle" data-feather="shopping-cart" style="color:red"></i></a></td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </div>
                  <?php	
                  }	
                  if($menu_item['layer']==2) {
                    if($l2_id>0) { //закрываем 2-й уровень ?>
                      </div>
                    <?php
                    }	
                    $l2_id = $menu_item['id'];
 ?>

                    <div class="card-header alert-primary" id="headingOne">
                      <h5 class="card-title my-2">
                        <a href="#" data-toggle="collapse" data-target="#collapse-<?=$i?>" aria-expanded="true" aria-controls="collapseOne" style="color:white"><?=$menu_item['name']?></a>
                      </h5>
                    </div>
                    <div id="collapse-<?=$i?>" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
                  <?php
                  }	
                  ?>
                  <?php
                  if($menu_item['layer']==1) {
                    if($l2_id>0) { //закрываем 2-й уровень ?>

                    <?php
                    }		
                    if($l1_id>0) { //закрываем 1-й уровень ?>

                    <?php
                    }
                    $l1_id = $menu_item['id'];
                    $l2_id=-1;	
                    ?>	
                    <div class="card">
                      <div class="card-header">
                        <h5 class="card-title mb-0"><?=$menu_item['name']?></h5>
                      </div>
                  <?php
                  }
                  $i++;
                }
                ?>
              </div>
            </div>
          </div>

The problem is that I can't properly close the tags I need. I do not understand.
The working HTML template has the following code
<div class="card">
                  <div class="card-header">
                    <h5 class="card-title mb-0">Родитель</h5>
                  </div>
                  <div class="card-header alert-primary" id="headingOne">
                    <h5 class="card-title my-2">
                      <a href="#" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne" style="color:white">Категория 1</a>
                    </h5>
                  </div>
                  <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordionExample">
                    <div class="card-body p-0 pt-1">
                      <h3 class="mb-0 text-center alert-primary" style="color:white">Подкатегория 1</h3>
                      <div class="table-responsive">
                        <table class="table table-sm mb-0">
                          <thead>
                            <tr>
                              <th scope="col"></th>
                              <th scope="col">Наименование</th>
                              <th scope="col">Описание</th>
                              <th scope="col">Цена завода (опт)</th>
                              <th scope="col">Цена базовая</th>
                              <th scope="col"></th>
                            </tr>
                          </thead>
                          <tbody>
                            <tr>
                              <td><div class="badge badge-danger my-2">Хит продаж</div></td>
                              <td><a href="#">Товар 1</a></td>
                              <td class="w-75">Описание</td>
                              <td>8 190 <i class="fas fa-ruble-sign"></i></td>
                              <td>9 635 <i class="fas fa-ruble-sign"></i></td>
                              <td class="table-action"><a href="#"><i class="align-middle" data-feather="shopping-cart" style="color:red"></i></a></td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div class="card-body p-0 pt-1">
                        <h3 class="mb-0 text-center alert-primary" style="color:white">Подкатегория 2</h3>
                        <div class="table-responsive">
                          <table class="table table-sm mb-0">
                            <thead>
                              <tr>
                                <th scope="col"></th>
                                <th scope="col">Наименование</th>
                                <th scope="col">Описание</th>
                                <th scope="col">Цена завода (опт)</th>
                                <th scope="col">Цена базовая</th>
                                <th scope="col"></th>
                              </tr>
                            </thead>
                            <tbody>
                              <tr>
                                <td><div class="badge badge-danger my-2">Хит продаж</div></td>
                                <td><a href="#">Товар 2</a></td>
                                <td class="w-75">Описание</td>
                                <td>8 190 <i class="fas fa-ruble-sign"></i></td>
                                <td>9 635 <i class="fas fa-ruble-sign"></i></td>
                                <td class="table-action"><a href="#"><i class="align-middle" data-feather="shopping-cart" style="color:red"></i></a></td>
                              </tr>
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>

I'm trying to put the closing </div>
one here (because in my opinion it should close the tag <card>, but it closes <div>which main accordion)
if($l1_id>0) { //закрываем 1-й уровень ?>
</div>
<?php
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Panaitov, 2020-05-28
@arsenaljek

5ecfdba7e78cf393079034.png
(1) this condition is not met, since the variable is filled after the condition (2)
Perhaps it is worth moving such variables to the global scope (before the foreach construct) so that they are not reset to zero in each iteration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question