I have a function displaying a menu on my webpage. I made it a little while ago, and it seemed to be working just fine, although when I added more submenu-items, it all went haywire. The purpose of the function is to display the main menu, and if there are submenu-items with the ID of the main menu item, these will display underneath. So far so good. What I want is to just show the current submenu-items - ie, I don't want to show them all, under each main menu item at the same time - they should only show up when and if the main-menu item of choice is clicked - and of course if any of the submenu-items is clicked. Right now, it ALMOST works. However, there is an error, and since I've looked so much at this code, and tweaked, edited, back-tracked and then re-tweaked again, I'm a little at a loss as to where the error is. The current problem is as follows: When entering any of the main menu items, the submenu-items for that particular item shows, just fine. The active menu item is colored, everything works. However, when clicking any of the submenu-items, EVERY submenu-item appears, although under their correct main menu heading. Working example: http://dev.tidsreiser.no <- (it redirects) Code I'm currently using: function display_menu() { global $menu_tbl,$page,$domain; $res = mysql_query("SELECT * FROM $menu_tbl WHERE sub_page = '' AND page_ID != 'admin' ORDER BY important DESC, priority ASC"); $numrows = mysql_numrows($res); ?> <div id="main_menu_wrapper"> <ul id="main_menu"> <?php $i=0; while ($i < $numrows) { $menu = mysql_fetch_array($res,MYSQL_BOTH); $mainID = $menu['ID']; $main_page = $menu['page_ID']; ?> <li id="<?php echo $menu['page_ID']; ?>_list" class="main_list"><a id="<?php echo $menu['page_ID']; ?>" href="<?php echo $domain; if ($menu['page_ID'] == 'index') { ?>/tidsreiser/web/index.php<?php } else { ?>/tidsreiser/web/index.php?page=<?php echo $menu['page_ID']; } ?>" title="<?php echo bbcode_to_html($menu['page_desc']); ?>"><?php echo bbcode_to_html(strtoupper($menu['page_name'])); ?></a></li> <?php $sub = mysql_query("SELECT * FROM $menu_tbl WHERE sub_ID = '$mainID' && sub_ID != 0 ORDER BY important DESC, priority ASC"); $numsub = mysql_numrows($sub); if ($numsub != '0') { $is=0; while ($is < $numsub) { $subcheck = mysql_fetch_array($sub,MYSQL_BOTH); $is++; }} if ($numsub == '0') {} elseif ((isset($_GET['subpage'])) || ($main_page == $page)) { ?> <li class="sub_menu_main"><ul> <?php $ii=0; mysql_data_seek($sub,0); while ($ii < $numsub) { $submenu = mysql_fetch_array($sub,MYSQL_BOTH); if ($submenu['page_ID'] == 'forum') { ?> <li id="<?php echo $submenu['page_ID']; ?>_list" class="main_list"><a id="<?php echo $submenu['page_ID']; ?>" href="<?php echo $domain; ?>/tidsreiser/web/forum/" title="<?php echo bbcode_to_html($submenu['page_desc']); ?>"><?php echo bbcode_to_html(strtoupper($submenu['page_name'])); ?></a></li> <?php } else { ?> <li id="<?php echo $submenu['page_ID']; ?>_list" class="main_list"><a id="<?php echo $submenu['page_ID']; ?>" href="<?php echo $domain; ?>/tidsreiser/web/index.php?page=<?php echo $submenu['page_ID']; ?>&subpage=<?php echo $menu['page_ID']; ?>" title="<?php echo bbcode_to_html($submenu['page_desc']); ?>"><?php echo bbcode_to_html(strtoupper($submenu['page_name'])); ?></a></li> <?php } ?> <?php $ii++; } ?> </ul></li> <?php } $i++; } ?> </ul> </div> <?php } PHP: If anyone can help, gimme a hint or two, I'd be really grateful!
Small typo on line 5? mysql_numrows should be mysql_num_rows right? unless you've created your own function.
mysql_numrows == mysql_num_rows - it's just that mysql_numrows is deprecated, and I'm trying to kick the habit. Does not affect the code, though. Any other suggestions?
Install the FireBug FireFox plugin and open the URL of a page that has your menu. It should display any HTML errors that occur on the page. My guess is that you have special characters in your database content such as < and > that would break the HTML. Any output should be wrapped in the htmlentities() function to ensure proper display.
Uhm, what? This has nothing to do with the HTML output - the HTML-output is working just fine - this has to do with the PHP parsing - ie. I'm not coding the parsing correctly, and I was hoping for someone to tell me where I'm doing it wrong, and what I might to do fix it. I was going to say that my page is valid XHTML Strict, but I would be lying - I'm using the "rel"-attribute on other HTML-tags than <a>/<link> so it does not validate - however, this has nothing to do with the error represented here in this case - this has to do with how the I'm getting data from the database, and displaying it - and is not an HTML error.
Since this is only happening on pages where $_GET['subpage'] is set, I think the problem must be with this line: elseif ((isset($_GET['subpage'])) || ($main_page == $page)) { ?> PHP: Isn't the first part of this line saying "show the submenus if subpage is in the URL"? You need some sort of comparison operator here, no? I'm not sure I understand all your variables, but something like: elseif (($_GET['subpage'] == $mainID) || ($main_page == $page)) { ?> PHP:
The $_GET['subpage'] is basically which main page the sub-menu item is for - ie if $_GET['subpage'] == 'index' the submenu is supposed to show up beneath the "index"-part on the menu - which it does. The problem lies in that when I click one of the submenu-items, for instance "about", then all of the submenues expand, regardless of which main page they're under. The "subpage" is not equal to "mainID" though - the subpage is a string, while the mainID is a numerical value (int). I tried doing: elseif (($_GET['subpage'] == $main_page) || ($main_page == $page)) { ?> PHP: but it didn't really work either. The problem is, as far as I can see, that the submenues, when used/highlighted/clicked expands all submenu items, instead of just the "active" part of the menu.
I'm bumping this, 'cause I could really use some help here. Still regarding the code in the first post. My tree-menu doesn't work entirely as planned, and I'm not really sure why or what I need to do to fix it. Anyone able to look into it would have my greatest thanks.