Hello!
Just wondering if anyone could point me in the right direction with this poser...
I am trying to construct a tree menu where users can search by 'topic' or by 'date'. This means that most of the options get repeated at least once. eg:
Search by topic
Population
1 August 2004
2 August 2005
AIDS
3 August 2004
4 August 2005
Search by date
2004
1 August 2004
3 August 2004
2005
2 August 2005
4 August 2005
In my head tag I have the following javascript:
<script type="text/javascript">
<!--
function toggle(id) {
if(document.getElementById) {
var el = document.getElementById(id);
el.style.display = (el.style.display == 'none') ? 'block' : 'none';
}
}
//-->
</script>
And where I want my menu I currently have:
<?php
// menu title => menu target
$menu = array(
'Population'=>array(
'14 October'=>'content.php?yr=04&id=1014',
'16 May'=>'content.php?yr=05&id=0516'),
'Debt'=>array(
'19 August'=>'content.php?yr=04&id=0819',
'18 August'=>'content.php?yr=04&id=0818'),
'AIDS'=>array(
'14 October'=>'content.php?yr=04&id=1014',
'16 May'=>'content.php?yr=05&id=0516'),
'Gender'=>array(
'19 August'=>'content.php?yr=04&id=0819',
'18 August'=>'content.php?yr=04&id=0818'),
'General'=>array(
'8 August'=>'content.php?yr=04&id=0808',
'31 July'=>'content.php?yr=04&id=0731'),
'1995'=>array(
'14 October'=>'content.php?yr=04&id=1014',
'16 May'=>'content.php?yr=05&id=0516'),
'2004'=>array(
'19 August'=>'content.php?yr=04&id=0819',
'18 August'=>'content.php?yr=04&id=0818'),
'2005'=>array(
'8 August'=>'content.php?yr=04&id=0808',
'31 July'=>'content.php?yr=04&id=0731')
);
// alternative method to using cookies
function array_search_recursive($needle, $haystack) {
$pos = null;
$keys = array_keys($haystack);
while(!$pos && (list($garbage, $value)=each($keys))) {
if(is_scalar($haystack[$value])) {
if($haystack[$value] === $needle)
$pos[] = $value;
} elseif(is_array($haystack[$value])) {
if($pos = array_search_recursive($needle, $haystack[$value]))
array_unshift($pos, $value);
}
}
return $pos;
}
// recursive function to draw menu
function draw_menu($menu, $preserve, &$id) {
if($id == 0)
echo "<div id=\"$id\">\r\n<ul>\r\n";
else
echo "<div id=\"$id\" style=\"display:none;\">\r\n<ul>\r\n";
$id += 1;
foreach($menu as $key=>$value) {
if(is_array($value)) {
if(@in_array($key, $preserve))
$toggle = $id;
echo "<li class=\"top\"><a href=\"#\" onclick=\"toggle($id);\">$key</a></li>\r\n";
draw_menu($value, $preserve, $id);
}
else {
echo "<li>";
if(@in_array($key, $preserve))
echo "<a href=\"$value\">$key</a>";
else
echo "<a href=\"$value\">$key</a>";
echo "</li>\r\n";
}
}
echo "</ul>\r\n</div>\r\n";
if(isset($toggle))
echo "<script language=\"javascript\">toggle($toggle);</script>\r\n";
}
$id = 0;
$base = basename($_SERVER['PHP_SELF']);
$self = isset($_SERVER['QUERY_STRING']) ? $base.'?'.$_SERVER['QUERY_STRING'] : $base;
$preserve = array_search_recursive($self, $menu);
draw_menu($menu, $preserve, $id);
?>
My basic problem is that every time I try and open a page that's mentioned already in the menu it all gets confused.
You can see what I mean here:
http://www.gerrydanaher.com/index.php (NB. the pages used are just as an example)
I'm a relative newbie to all this and have been looking for a solution for a whole day now, but i think half the problem is i don't know what search terms i should be using to get a relevant result.
I'd be extremely grateful for ANY help whatsoever that you experts could offer - please rescue me from this menu!!!
from Lizzyd