Jump to content

Php navigation - changing background colour when active


cherbert

Recommended Posts

I have got a code working to generate a menu from a mysql table, but I wan't to know if it is possible to have the background colour change to a third option when active.

I have 1 background colour for 'off', one for mouseover, but can't figure out how to tell it what they current page is so that it will use a different css class value, as the page obviously reloads after the link is clicked. Is it possible (hope my explanation makes some sence!)

 

My code:

At the moment mainNav a: and mainNav a:hover work ok but what I want is a mainNav a:active to stay on after the page has loaded

<?php
//function to nest the menu tree
function nest_child(&$nodes, $current) {
	if (!isset($nodes) || count($nodes) < 1) {
		return;
	}
	foreach ($nodes as $id => $item) {
		if ($current['parentdocid'] == $id) {
			if (!isset($item['children'])) {
				$item['children'] = array();
			}
			array_push($item['children'], $current);
			$nodes[$id] = $item;
		} else {
			nest_child($item['children'], $current);
		}
	}
}

//functions to display the tree and expand as appropriate
function menu_display($nodes, $display_id) {
	if (!isset($display_id) || ($display_id == '')) {
		$display_id = 1;
	}
	$result = node_display(
		array(
			'doc_id'   => 1,
			'children' => $nodes
		),
		$display_id
	);
	return $result[1];
}
function node_display($node, $display_id) {
	global $ROOT;
	$found = false;
	$text = '';

	if (isset($node[children])) {
		foreach ($node[children] as $id => $child) {
			$text .= "<li class='mainNav'><a href='$ROOT/index.php?doc_id=$child[doc_id]'>$child[doc_name]</a>";
			$result = node_display($child, $display_id);
			if ($result[0]) {
				$found = true;
				$text .= "<ol>$result[1]</ol>";
			}
			if ($child[doc_id] == $display_id) {
				$found = true;
				if (isset($child[children])) {
					$text .= "<ol>";
					foreach ($child[children] as $id => $grandchild) {
						$text .= "<li class='mainNav'><a href='$ROOT/index.php?doc_id=$grandchild[doc_id]'>$grandchild[doc_name]</a>";
					}
					$text .= "</ol>";
				}
			}
			$text .= "</li>";
		}
	}
	if ((!$found) && ($display_id != 1)) {
		$text = '';
	}

	return array($found, $text);
}
?>

<ul class="menu">
<?php
	//retrieve all the menu elements
	$result = mysql_query('SELECT * FROM docs ORDER BY doc_order ASC');
	$menu = array();
	while ($row = mysql_fetch_assoc($result)) {
		$menu[$row['doc_id']] = $row;
	}

	//nest the elements
	foreach ($menu as $id => $item) {
		if ($item['parentdocid'] != 1) {
			nest_child($menu, $item);
			unset($menu[$id]);
		}
	}

	//display the tree
	print menu_display($menu, $doc_id);
?>
</ul>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.