Question

I'm still having trouble with adding a ternary operator to my menu. It's purpose is to add class=active to the li tag if the menuid and page id is the same. The pageID is stored in a var called thisPage... so when thisPage and menuID are equal I should get the class tag.

I've included the code and a php dump to help determine the issue

The line of code I have issue with is the ternary operator in the ///SUB MENU & ///MAIN MENU sections starting with

($thisPage==$sval['menuID'] ? ' class="active"' : '')  

I think the "$sval['menuID']" is the problem.

 <?php

$thisPage=3; //

    $sql = "SELECT menuID, menuTitle, menuURL, menuParentID, menuOrderID FROM menu ORDER BY menuParentID, menuOrderID ASC";
    $items = mysql_query($sql);
    while ($obj = mysql_fetch_object($items)) {
        if ($obj->menuParentID == 0) {
            $parent_menu[$obj->menuID]['menuID'] = $obj->menuID;
            $parent_menu[$obj->menuID]['menuTitle'] = $obj->menuTitle;
            $parent_menu[$obj->menuID]['link'] = $obj->menuURL;
        } else {
            $sub_menu[$obj->menuID]['menuID'] = $obj->menuID;
            $sub_menu[$obj->menuID]['parent'] = $obj->menuParentID;
            $sub_menu[$obj->menuID]['menuTitle'] = $obj->menuTitle;
            $sub_menu[$obj->menuID]['link'] = $obj->menuURL;
            if (!isset($parent_menu[$obj->menuParentID]['count'])) {
                $parent_menu[$obj->menuParentID]['count'] = 0;
            }
            $parent_menu[$obj->menuParentID]['count']++;
        }
    }

    mysql_free_result($items);


    function create_menu($parent_array, $sub_array, $qs_val = "menu", $main_id = "nav", $sub_id = "dropdown-menu", $dd_style = "dropdown")
    {
        $menu = "<ul class=\"{$main_id}\">\n";

        foreach ($parent_array as $pkey => $pval) {
            if ( ! empty($pval['count'])) {

                            //DROP-DOWN MENUS
                $menu .= "\t<li class=\"{$dd_style}\"><a href=\"{$pval['link']}\" class=\"dropdown-toggle\" data-toggle=\"$dd_style\">{$pval['menuID']} - {$pval['menuTitle']}<b class=\"caret\"></b></a>\n";
                $menu .= "\t\t<ul class=\"{$sub_id}\">\n";

                foreach ($sub_array as $sval) {
                    if ($pkey == $sval['parent']) {

///////////////////////////////////////SUB MENUS
                        $menu .= "\t\t\t<li" . ($thisPage==$sval['menuID'] ? ' class="active"' : '') . "><a href=\"{$sval['link']}\">{$sval['menuID']} - {$sval['menuTitle']}</a></li>\n";
                    }
                }
                $menu .= "</ul></li>\n";
            } else {

////////////////////////MAIN MENUS
                $menu .= "\t<li" . ($thisPage==$pval['menuID'] ? ' class="active"' : '') . "><a href=\"{$pval['link']}\">{$pval['menuID']} - {$pval['menuTitle']}</a></li>\n";
            }
        }

        return $menu . "</ul>\n";
    }

    ?>
    <?php 
    echo create_menu($parent_menu, $sub_menu, "menu", "nav", "dropdown-menu");
    ?>


   ////////////////////////////////
   //////////// PHP DUMP///////////
   ////////////////////////////////

   //PAGE QUERY
        [query_rsPageContent] => SELECT * FROM pages WHERE pageID = 3
        [rsPageContent] => Resource id #4
        [row_rsPageContent] => Array
            (
                [pageID] => 3
                [menuParentID] => 0
                [pageRbnTitle] => Ministries
                [pageTitle] => Ministries
                [pageContent] => Ministries Content
                [pageDescripition] => 
                [pageKeywords] => 
            )

        [totalRows_rsPageContent] => 1

        [thisPage] => 3

    //MENU QUERY
        [sql] => SELECT menuID, menuTitle, menuURL, menuParentID, menuOrderID FROM menu ORDER BY menuParentID, menuOrderID ASC
        [items] => Resource id #7
        [obj] => 
        [parent_menu] => Array
    (
        [1] => Array
            (
                [menuID] => 1
                [menuTitle] => Title 1
                [link] => 1.php
            )

        [2] => Array
            (
                [menuID] => 2
                [menuTitle] => Title 2
                [link] => #
                [count] => 1
            )

        [3] => Array
            (
                [menuID] => 3
                [menuTitle] => Title 3
                [link] => 3.php
            )
)
Was it helpful?

Solution

$thisPage is not known in context of create_menu function. Pass it as an argument or use global $thisPage if absolutely necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top