2

I have 2 database table. One is for menu name and another is for child menu name. Some menus have no child menu. I want to create a navigation menu where it will check either the menu has children or not. If it has children it will add data-toggle="dropdown" to that menu so the child menu can be seen in the dropdown box. Here is my code, I am not getting the idea why my code is not working or how my logic will be in HTML.

function myFunction()
{
  $(".dropdown-toggle").attr("data-toggle", "dropdown");
}
    
<nav class="navbar fixed-top navbar-expand-lg navbar-dark bg-dark fixed-top">
        <a class="navbar-brand" href="<?php echo base_url('welcome/index')?>">
            <img src="<?php echo base_url()?>assets/images/logos/logo.png" alt="LogoFOR2401" title="LogoFOR2401">
        </a>
        <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav ml-auto">
                <?php foreach ($navigationmenu as $navigationmenu_item): ?>
                    <li class="nav-item">
                        <a href="<?php echo base_url('welcome/menu')?>/<?php echo $navigationmenu_item['MenuID']; ?>" class="nav-link dropdown-toggle" name="<?php echo $navigationmenu_item['MenuName']; ?>" id="navbarDropdownBlog" aria-haspopup="true" aria-expanded="false"><?php echo $navigationmenu_item['MenuName']; ?></a>                      
                        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog">
                            <?php foreach ($submenu as $submenu_item): ?>
                                <?php if($navigationmenu_item['MenuID'] == $submenu_item['MenuID']) {?>        
                                    echo "<script type="text/javascript">myFunction();</script>";
                                    <!--echo "function myFunction();";-->
                                    <!--echo "</script>";-->
                                    <a class="dropdown-item" href="<?php echo base_url('welcome/submenu')?>/<?php echo $submenu_item['SubMenuID']; ?>"><?php echo $submenu_item['SubMenuName']; ?></a>
                                <?php } ?>  
                            <?php endforeach; ?>
                        </div>
                    </li>
                <?php endforeach; ?>
            </ul>
        </div>
    </nav>

3
  • why are you injecting Javascript to do this? It should just be some fairly simple PHP. And the script will do this action to all elements with the dropdown-toggle class anyway, not just one. Commented Sep 7, 2017 at 15:31
  • Could you please show me an example for this? @ADyson Commented Sep 7, 2017 at 15:32
  • Possible duplicate of Adding attributes to HTML tags with PHP Commented Sep 7, 2017 at 15:38

1 Answer 1

3

There are several problems.

  1. Since you exited from script execution mode with ?>, you shouldn't use echo. Just put the HTML there.
  2. Your function doesn't add the class only to the current menu, it adds the class to all preceding elements with class="dropdown-toggle".
  3. You're using id="navbarDropdownBlog" each time through the loop. IDs should be unique.

You should do the check in PHP, rather than running a function to add the class.

<?php foreach ($navigationmenu as $navigationmenu_item):
    $submenus = array_filter($submenu, function($submenu_item) use ($navigationmenu_item) {
        return $submenu_item['MenuID'] == $navigationmenu_item['MenuID'];
    });
    $data = empty($submenus) ? '' : 'data-toggle="dropdown"';
    ?>
    <li class="nav-item">
        <a href="<?php echo base_url('welcome/menu')?>/<?php echo $navigationmenu_item['MenuID']; ?>" class="nav-link dropdown-toggle" name="<?php echo $navigationmenu_item['MenuName']; ?>" id="navbarDropdownBlog-<?php echo $navigationmenu_item['MenuID']; ?>" aria-haspopup="true" aria-expanded="false" <?php echo $data; ?>><?php echo $navigationmenu_item['MenuName']; ?></a>                      
        <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownBlog-<?php echo $navigationmenu_item['MenuID']; ?>">
            <?php foreach ($submenus as $submenu_item): ?>
                <a class="dropdown-item" href="<?php echo base_url('welcome/submenu')?>/<?php echo $submenu_item['SubMenuID']; ?>"><?php echo $submenu_item['SubMenuName']; ?></a>
            <?php endforeach; ?>
        </div>
    </li>
<?php endforeach; ?>

To solve problem 3, I appended the menu ID to the ID.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, it solves my main problem. Thanks for your help. @Barmar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.