Generate menu and submenu with PHP

Asked

Viewed 3,367 times

3

Situation:

I am building a menu with submenu of information that retrieves from the database, these data are categories and subcategories of products, this menu is ready and working but when there are no subcategories (that is a submenu of the menu) this generating a <ul> empty.

Technologies involved:

  • Cakephp 3.0 retrieves data from the database through its ORM. (I find it hardly impactful on the problem).
  • Cakephp Standard Templates with Extension .ctp.
  • PHP used to render menu and submenu.

SQL of the category and subcategory tables

CREATE TABLE categories (
  id INT AUTO_INCREMENT PRIMARY KEY,
  category_name VARCHAR(255) NOT NULL,
  created DATETIME,
  modified DATETIME
);

CREATE TABLE sub_categories (
  id INT AUTO_INCREMENT PRIMARY KEY,
  sub_category_name VARCHAR(255) NOT NULL,
  category_id INT NOT NULL,
  created DATETIME,
  modified DATETIME,
  FOREIGN KEY category_key (category_id) REFERENCES categories(id)
);

Template generating menu and submenu

<div id="left-sidebar" class="col-md-2">
    <ul class="nav nav-pills nav-stacked">
    <?php foreach($categories as $category): ?>
        <li role="presentation">
            <a href="#"> <?= $category ?>
            <i class="glyphicon glyphicon-chevron-right"></i></a>
            <ul>
                <?php foreach($subCategories as $subCategory): ?>
                    <?php if($subCategory['category']['category_name'] == $category): ?>
                        <li role="presentation">
                            <a href="#"> <?= $subCategory['sub_category_name'] ?></a>
                        </li>
                    <?php endif; ?>
                <?php endforeach; ?>
            </ul>
        </li>
    <?php endforeach; ?>
    </ul>
</div>

OBS $categories is a simple string array and $subCategories is an object array (which has as one of its properties an object representing a category where a property of this object and the category name).

Menu photo and submenu (accent problem is due to mysql collation, I will change later).

inserir a descrição da imagem aqui

Menu rendered in browser

<ul class="nav nav-pills nav-stacked">
    <li role="presentation">
        <a href="#"> Alimentos<i class="glyphicon glyphicon-chevron-right"></i></a>
        <ul>
            <li role="presentation">
                <a href="#"> Industrilizado</a>
            </li>
            <li role="presentation">
                <a href="#"> In natura</a>
            </li>
            <li role="presentation">
                <a href="#"> Comida pronta</a>
            </li>
        </ul>                                                                                                                                                                                      </ul>
    </li>
    <li role="presentation">
        <a href="#"> Bazar<i class="glyphicon glyphicon-chevron-right"></i></a>
        <ul>
        </ul>
    </li>
</ul>

1 answer

3

You’re one short if check whether there are subcategories:

  <?php if(/*existem subcategorias*/){ ?>
        <ul>
            <?php foreach($subCategories as $subCategory): ?>
                <?php if($subCategory['category']['category_name'] == $category): ?>
                    <li role="presentation">
                        <a href="#"> <?= $subCategory['sub_category_name'] ?></a>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
  <?php } ?>

To <ul> will always appear, if you don’t want any you have to check before the <ul> if there are subcategories.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.