Problem with PHP-based tree-menu

Discussion in 'PHP' started by PoPSiCLe, May 31, 2009.

  1. #1
    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']; ?>&amp;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!
     
    PoPSiCLe, May 31, 2009 IP
  2. dannywwww

    dannywwww Well-Known Member

    Messages:
    804
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Small typo on line 5?
    mysql_numrows should be mysql_num_rows right? unless you've created your own function.
     
    dannywwww, May 31, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    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?
     
    PoPSiCLe, May 31, 2009 IP
  4. johnkramlich

    johnkramlich Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    johnkramlich, May 31, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    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.
     
    PoPSiCLe, May 31, 2009 IP
  6. JDevereux

    JDevereux Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    JDevereux, May 31, 2009 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    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.
     
    PoPSiCLe, Jun 1, 2009 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    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.
     
    PoPSiCLe, Jun 2, 2009 IP