php - ternary operator in a dynamic dropdown menu -
i'm still having trouble adding ternary operator menu. it's purpose add class=active li tag if menuid , page id same. pageid stored in var called thispage... when thispage , menuid equal should class tag.
i've included code , php dump determine issue
the line of code have issue ternary operator in ///sub menu & ///main menu sections starting
($thispage==$sval['menuid'] ? ' class="active"' : '')
i think "$sval['menuid']" problem.
<?php
$thispage=3; //
$sql = "select menuid, menutitle, menuurl, menuparentid, menuorderid menu order 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 $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 $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 * pages 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 menu order 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 ) )
$thispage
not known in context of create_menu
function. pass argument or use global $thispage
if absolutely necessary.
Comments
Post a Comment