Generate menu and submenu with PHP

3

Location:

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

Technologies involved:

  • CakePHP 3.0 that retrieves the data from the database through its ORM. (I find it a little shocking in the problem).
  • CakePHP default templates with .ctp extension.
  • PHP used to render the 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 that generates 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 array of objects (which has as one of its properties an object representing a category where a property of this object is the category name).

Photo menu and submenu (the accent problem is due to the collation of mysql, I will change later).

Browserrenderedmenu

<ulclass="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>
    
asked by anonymous 27.07.2015 / 16:20

1 answer

3

You will not be able to see%% check if subcategories exist:

  <?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 } ?>

The if will always appear, if you do not want any, you must check before <ul> if subcategories exist.

    
27.07.2015 / 16:24