Dropdown Menu Browse By Year

Through a combination of some css, and the subnav feature of the nav function (LinkFunctions) I've been working on getting drop-down menus, in my header's navigation. I've figured the basics, but now I have an issue where I want to have a browse by year drop-down menu. You hover over the browse by year, and you get a list of decades, you mouse over a decade, and then a list of years pops up. It works, but I'd like to consolidate my code. Here's what I currently have:

<ul class="dropdown">
<?php
$links = array('Home' => uri(''),
'Browse Items' => array('uri' => 'items',
           'subnav_links' => array(
'Oldest First' => 'items/browse?sort_field=Dublin+Core,Date',
'Newest First' => 'items/browse?sort_field=Dublin+Core,Date&sort_dir=d'),
'subnav_attributes' => array('class' => 'subnav')),
'Collections' => array('uri'=>'#', 'subnav_links' => array(
'People' => 'items/browse?collection=4',
'Historic Images ' => uri('items/browse?collection=1'),
'Community Contributions'=> uri('items/browse?collection=3')),
          'subnav_attributes' => array('class' => 'subnav')),
'Tags' => uri('items/tags?sort=alpha'),
'Add Your Own'=>uri('contribution'),
'About'=>uri('about'),
'Help'=>uri('help'),
'Browse By Year'=>array('uri'=>'#',
'subnav_links'=>array('1900s'=>array('uri'=>'#',
'subnav_links'=>array(
'1900'=>'items/browse?search=1900',
'1901'=> 'items/browse?search=1901',
'1902'=> 'items/browse?search=1902',
'1903'=> 'items/browse?search=1903',
'1904'=> 'items/browse?search=1904',
'1905'=> 'items/browse?search=1905',
'1906'=> 'items/browse?search=1906',
'1907'=> 'items/browse?search=1907',
'1908'=> 'items/browse?search=1908',
'1909'=> 'items/browse?search=1909'),
'subnav_attributes' => array('class' => 'subnav')),

<!-- Code here from 1910 to 1999 -->

'2000s'=>array('uri'=>'#',
'subnav_links'=>array(
'2000'=>'items/browse?search=2000',
'2001'=> 'items/browse?search=2001',
'2002'=> 'items/browse?search=2002',
'2003'=> 'items/browse?search=2003',
'2004'=> 'items/browse?search=2004',
'2005'=> 'items/browse?search=2005',
'2006'=> 'items/browse?search=2006',
'2007'=> 'items/browse?search=2007',
'2008'=> 'items/browse?search=2008',
'2009'=> 'items/browse?search=2009'),
'subnav_attributes' => array('class' => 'subnav'))),'subnav_attributes'=>array('class'=>'decade')));
echo nav($links,4); ?></ul>

However instead of writing that all out I'd like to use something like

<?php
$yearlist="array(";
foreach (range(1900, 2009) as $number) {
$last=substr($number,-1);
if ($last==0){$yearlist.= "'".$number."s'=>array('uri'=>'#', 'subnav_links'=>array('".$number."'=>'/throughtime/items/browse?search=".$number."',";
}
else if ($last==9){$yearlist.= "'".$number."'=> '/throughtime/items/browse?search=".$number."'),'subnav_attributes' => array('class' => 'subnav')),";
}
else{
$yearlist.= "'".$number."'=> '/throughtime/items/browse?search=".$number."',";
}
}
$yearlist=rtrim($yearlist,",");
$yearlist.="),'subnav_attributes'=>array('class'=>'year');"; ?>

This generates the string for the code above for all the years. I'd like to insert the variable $yearlist into 'Browse By Year'=>array('uri'=>'#', 'subnav_links'=>$yearlist, but I haven't figured it out. How do I insert a variable like that into the $links variable that's used in the nav function.?

You're trying to build up this array as a string. This way, you'd end up needing to use eval or some other evilness, and you'd end up with crazy code. Instead, just work on your arrays directly with the array() and [] constructs.

//Your original code for $links, excluding the Browse by Year portion
// ...

$yearNav = array();
for ($decade = 1900; $decade <= 2000; $decade += 10) {
    $decadeSublinks = array();
    for ($year = $decade; $year <= $decade + 9; $year++) {
        $decadeSublinks[$year] = uri('items/browse', array('search' => $year));
    }
    $yearNav[$decade . 's'] = array(
        'uri' => '#',
        'subnav_links' => $decadeSublinks,
        'subnav_attributes' => array('class' => 'subnav'));
}

$links['Browse by Year'] = array(
    'uri' => '#',
    'subnav_links' => $yearNav,
    'subnav_attributes' => array('class' => 'year'));

Thanks John, I'll see if I can get it working. I figured I was making things more complicated than they needed to be.

Here's what I've got now, and it works. I moved the Browse by Year from the end of the list of tabs to the middle, so I used the array() and [] constructs for the remaining tabs. Also I realized just using a simple search for the date returned results that had the date anywhere in the record, so I've changed it to an advanced search. However, is there a way to write an advanced search like you have for the simple search: $decadeSublinks[$year] = uri('items/browse', array('search' => $year));?

<ul class="testing">
<?php
$links = array('Home' => uri(''),
'Browse Items' => array('uri' => 'items',
           'subnav_links' => array(
'Oldest First' => 'items/browse?sort_field=Dublin+Core,Date',
'Newest First' => 'items/browse?sort_field=Dublin+Core,Date&sort_dir=d'),
'subnav_attributes' => array('class' => 'subnav')),
'Collections' => array('uri'=>'#',
           'subnav_links' => array(
'VUMC People' => 'items/browse?collection=4',
'Historic Images of VUMC' => uri('items/browse?collection=1'),
'Community Contributions'=> uri('items/browse?collection=3')),
'subnav_attributes' => array('class' => 'subnav')),
'Tags' => uri('items/tags?sort=alpha'));

$yearNav = array();
for ($decade = 1900; $decade <= 2000; $decade += 10) {
    $decadeSublinks = array();
    for ($year = $decade; $year <= $decade + 9; $year++) {
        $decadeSublinks[$year] = uri('items/browse?search=&advanced[0][element_id]=40&advanced[0][type]=contains&advanced[0][terms]='.$year.'&collection=&user=&tags=&public=&featured=&submit_search=Search');
    }
$yearNav[$decade . 's'] = array(
        'uri' => '#',
        'subnav_links' => $decadeSublinks,
        'subnav_attributes' => array('class' => 'subnav'));
}

$links['Browse by Year'] = array(
    'uri' => '#',
    'subnav_links' => $yearNav,
    'subnav_attributes' => array('class' => 'year'));

$links ['Add Your Own'] = array('uri'=>'contribution');
$links ['About'] = array('uri'=>'about');
$links ['Help'] = array('uri'=>'help');

echo nav($links,4); ?></ul>

The advanced search just needs a more deeply-nested array in that second parameter:

uri('items/browse', array('advanced' => array(
    array(
        'element_id' => 40,
        'type' => 'contains',
        'terms' => $year
    ))));

Awesome. Thanks again.