FightForJustice
FightForJustice

Reputation: 355

Bootstrap dropdown with cakephp not working

I am using http://www.bootply.com/nZaxpxfiXz for a drop down list. But my data set is like

array(
    (int) 0 => array(
        'Genre' => array(
            'id' => '1',
            'name' => 'Alternative Rock',
            'is_active' => true,
            'updated' => '2015-10-03 19:19:39',
            'parent_id' => '24'
        )
    ),
    (int) 1 => array(
        'Genre' => array(
            'id' => '2',
            'name' => 'College Rock',
            'is_active' => true,
            'updated' => '2015-10-03 19:19:45',
            'parent_id' => '24'
        )
    ),
    (int) 2 => array(
        'Genre' => array(
            'id' => '3',
            'name' => 'Experimental Rock',
            'is_active' => true,
            'updated' => '2015-10-03 19:19:50',
            'parent_id' => '24'
        )
    ),
    (int) 3 => array(
        'Genre' => array(
            'id' => '4',
            'name' => 'Goth Rock',
            'is_active' => true,
            'updated' => '2015-10-03 19:19:57',
            'parent_id' => '24'
        )
    ),
    (int) 4 => array(
        'Genre' => array(
            'id' => '5',
            'name' => 'Grunge',
            'is_active' => true,
            'updated' => '2015-01-29 17:16:15',
            'parent_id' => null
        )
    ),
    (int) 5 => array(
        'Genre' => array(
            'id' => '6',
            'name' => 'Hardcore Punk',
            'is_active' => true,
            'updated' => '2015-01-29 17:16:15',
            'parent_id' => null
        )
    ),
    (int) 6 => array(
        'Genre' => array(
            'id' => '7',
            'name' => 'Hard Rock',
            'is_active' => true,
            'updated' => '2015-01-29 17:16:15',
            'parent_id' => null
        )
    ),
    (int) 7 => array(
        'Genre' => array(
            'id' => '10',
            'name' => 'Pop',
            'is_active' => true,
            'updated' => '2015-10-03 16:38:43',
            'parent_id' => null
        )
    ),
    (int) 8 => array(
        'Genre' => array(
            'id' => '24',
            'name' => 'Rock',
            'is_active' => true,
            'updated' => '2015-10-03 19:19:32',
            'parent_id' => null
        )
    )
)

I want to build a drop down list with category and subcategories. Which have parent_id those will be sub category of a category.

How to fit my php array into this view ?

 <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Dropdown Link 1</a></li>
                        <li><a href="#">Dropdown Link 2</a></li>
                        <li><a href="#">Dropdown Link 3</a></li>
                        <li class="divider"></li>
                        <li class="dropdown dropdown-submenu"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown Link 4</a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Dropdown Submenu Link 4.1</a></li>
                                <li><a href="#">Dropdown Submenu Link 4.2</a></li>
                                <li><a href="#">Dropdown Submenu Link 4.3</a></li>
                                <li><a href="#">Dropdown Submenu Link 4.4</a></li>
                            </ul>
                        </li>

                    </ul>
                </li>

Upvotes: 2

Views: 790

Answers (1)

George
George

Reputation: 280

This should do it. But you'll probably need to rethink where you put that getChildren function (I included it inline for simplicity), you might want to make a CakePHP view helper class, or even perform that logic in your controller before getting to your view.

<li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
    <ul class="dropdown-menu">
        <?php foreach ($genres as $genre) : ?>
            <?php if ($genre['Genre']['parent_id'] == null) : ?>
                <?php $children = getChildren($genres, $genre['Genre']['id']); ?>
                <li class="<?php echo empty($children) ? '' : 'dropdown dropdown-submenu'; ?>">

                    <?php $a_string = 'class="dropdown-toggle" data-toggle="dropdown"'; ?>
                    <a href="#" <?php echo empty($children) ? '' : $a_string; ?>>
                        <?php echo $genre['Genre']['name']; ?>
                    </a>

                    <?php if (!empty($children)) : ?>
                        <ul class="dropdown-menu">
                            <?php foreach ($children as $child) : ?>
                                <li><a href="#"><?php echo $child['Genre']['name']; ?></a></li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</li>

<?php
    function getChildren($genres, $parent_id) {
        $children = array();
        foreach ($genres as $genre_inner) {
            if ($genre_inner['Genre']['parent_id'] == $parent_id) {
                $children[] = $genre_inner;
            }
        }
        return $children;
    }
?>

Upvotes: 1

Related Questions