oskarsl Posted July 26, 2019 Share Posted July 26, 2019 Hello! Can someone please explain to me why I do not read navigation from mysql database data? Database Code: <style type="text/css"> .navbar .dropdown-toggle, .navbar .dropdown-menu a { cursor: pointer; } .navbar .dropdown-item.active, .navbar .dropdown-item:active { color: inherit; text-decoration: none; background-color: inherit; } .navbar .dropdown-item:focus, .navbar .dropdown-item:hover { color: #16181b; text-decoration: none; background-color: #f8f9fa; } @media (min-width: 767px) { .navbar .dropdown-toggle:not(.nav-link)::after { display: inline-block; width: 0; height: 0; margin-left: .5em; vertical-align: 0; border-bottom: .3em solid transparent; border-top: .3em solid transparent; border-left: .3em solid; } } </style> <script type="text/javascript"> $(document).ready(function () { $('.navbar .dropdown-item').on('click', function (e) { var $el = $(this).children('.dropdown-toggle'); var $parent = $el.offsetParent(".dropdown-menu"); $(this).parent("li").toggleClass('open'); if (!$parent.parent().hasClass('navbar-nav')) { if ($parent.hasClass('show')) { $parent.removeClass('show'); $el.next().removeClass('show'); $el.next().css({"top": -999, "left": -999}); } else { $parent.parent().find('.show').removeClass('show'); $parent.addClass('show'); $el.next().addClass('show'); $el.next().css({"top": $el[0].offsetTop, "left": $parent.outerWidth() - 4}); } e.preventDefault(); e.stopPropagation(); } }); $('.navbar .dropdown').on('hidden.bs.dropdown', function () { $(this).find('li.dropdown').removeClass('show open'); $(this).find('ul.dropdown-menu').removeClass('show open'); }); }); </script> <?php $servername = "localhost"; $username = "root"; $password = ""; $db = new PDO("mysql:host=$servername;dbname=dbname", $username, $password); function menu_builder1($db, $parent_id) { $sql = $db->prepare("SELECT * FROM menu WHERE status = 1 ORDER BY position ASC"); if($sql->execute()) { while ($row = $sql->fetch(PDO::FETCH_ASSOC)) { $array[$row['menu_sub_id']][] = $row; } main_menu1($array); } } function main_menu1($array, $parent_id = false) { if(!empty($array[$parent_id])) { foreach ($array[$parent_id] as $item) { if ($item['dropdown'] == false) { echo '<li class="nav-item active"><a class="nav-link" href="' . $item['href'] . '">' . $item['name'] . '</a></li>'; } elseif ($item['dropdown'] == true) { echo '<li class="nav-item dropdown"><a class="nav-link dropdown-toggle" id="dropdown2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . $item['name'] . '</a>'; sub_menu1($array, $item['menu_id']); echo '</li>'; } } } } function sub_menu1($array = array(), $parent_id = false) { if(!empty($array[$parent_id])) { echo '<ul class="dropdown-menu" aria-labelledby="dropdown2">'; foreach ($array[$parent_id] as $item) { if ($item['dropdown'] == false) { echo '<li class="dropdown-item"><a href="' . $item['href'] . '">' . $item['name'] . '</a></li>'; } elseif ($item['dropdown'] == true) { echo '<li class="dropdown-item dropdown"><a class="dropdown-toggle" id="dropdown2-1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">' . $item['name'] . '</a>'; sub_sub_menu1($array, $item['menu_id']); echo '</li>'; } } echo "</ul>"; } } function sub_sub_menu1($array = array(), $parent_id = false) { if(!empty($array[$parent_id])) { echo '<ul class="dropdown-menu" aria-labelledby="dropdown2-1">'; foreach ($array[$parent_id] as $item) { if ($item['dropdown'] == false) { echo '<li class="dropdown-item"><a href="' . $item['href'] . '">' . $item['name'] . '</a></li>'; } } echo "</ul>"; } } ?> <div class="navbar navbar-expand-md navbar-dark bg-dark mb-4" role="navigation"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarCollapse"> <ul class="navbar-nav mr-auto"> <?=menu_builder1($db, 0)?> </ul> </div> </div> Result: Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/ Share on other sites More sharing options...
cyberRobot Posted July 26, 2019 Share Posted July 26, 2019 I haven't looked at all the code, but I'm guessing it has something to do with the following line: main_menu1($array); Since you're not passing $parent_id, the function defaults to "false". Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/#findComment-1568709 Share on other sites More sharing options...
Barand Posted July 26, 2019 Share Posted July 26, 2019 Is your dbname really "dbname"?. You might want to make some useful enhancements to your pdo connection code $db = new PDO("mysql:host=$servername;dbname=dbname", $username, $password); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // error reporting auto handled $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // saves specifying every time $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); // handle prepared queries correctly 1 hour ago, oskarsl said: $sql = $db->prepare("SELECT * FROM menu WHERE status = 1 ORDER BY position ASC"); In the above query there are no parameters so, instead of prepare/execute, just use $db->query(); Just my 0.02 worth. Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/#findComment-1568712 Share on other sites More sharing options...
ginerjm Posted July 26, 2019 Share Posted July 26, 2019 Two questions: 1 - What do you mean by "read navigation"? 2 - Your php code has a lot of functions but no "main line" code that makes use of them. Perhaps that is why you are posting here? Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/#findComment-1568713 Share on other sites More sharing options...
Barand Posted July 26, 2019 Share Posted July 26, 2019 12 minutes ago, ginerjm said: Your php code has a lot of functions but no "main line" code that makes use of them. You evidently missed this "main line" code when you read the initial post.... <ul class="navbar-nav mr-auto"> <?=menu_builder1($db, 0)?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/#findComment-1568714 Share on other sites More sharing options...
ginerjm Posted July 26, 2019 Share Posted July 26, 2019 Yes I did. Wasn't looking at a UL tag to contain that code.... Probably cause of the layout of this mish-mash of Html and php interspersed. Quote Link to comment https://forums.phpfreaks.com/topic/309030-mysqlphp-navigation/#findComment-1568715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.