1
foreach ($nav_items as $nav_href=>$nav_title=>$nav_class) {
    if ($pageHref == $nav_href) {
        echo '<li class="' . $nav_class .  'active">' . $nav_title . '</li>';
    } else {
        echo '<li class="' . $nav_class . '">' . $nav_title . '</li>';
    }
}

What is wrong with this code above? This corresponds to the array below.

$pageLoc = 'where';
$nav_items = array(
    'index'=>'Home'==>'', 
    'where'=>'Where?'==>'dropdown'==>'', 
    'appeals'=>'Current Appeals'==>'', 
    'news'=>'Latest News'==>'', 
    'events'=>'Events'==>'', 
    'dontate'=>'Dontate'==>'', 
);

I am basically trying to put an active class in front of the list item when it matches $pageLoc and when it doesn't just a simple <li class=""> BUT the dropdown menu item: 'where?' needs a <li class="dropdown"> always! and when it is active, it will be <li class="dropdown active">

2
  • 2
    You need to use nested arrays for this Commented Jun 14, 2013 at 17:05
  • all you'd need to do to figure out this won't work is ... run the code. you'll get an error that tells you exactly what's wrong (your syntax, in this case). Commented Jun 14, 2013 at 17:13

4 Answers 4

2

You have a flat-out syntax error:

foreach ($nav_items as $nav_href=>$nav_title=>$nav_class) {
                                            ^^^^^^^^^^^^

The syntax is

foreach($array as $key => $value)

You cannot have => $value1 => $value2 etc....

Sign up to request clarification or add additional context in comments.

2 Comments

Any ideas on how to go about solving my issue? I have a menu with active classes. But for one menu item, I need a dropdown class always to preside there.
$nav_href=>$nav_title in your foreach, then grab them from within the foreach loop
0

You ca n have it as :

foreach ($nav_items as $nav_title=>$nav_class) {

  ....  

}

What are you trying to check with $nav_href ?

2 Comments

It is for a navbar menu. So they all have normal <li></li>. When on that page, eg when $nav_href= 'current-page' the li class for that menu item will be 'active' and the others remain at <li>. My problem is, I have one menu item that requires a dropdown class always.
Why dont you check this way ? if($pageLoc == $nav_class) because your $nav_class is the key in the key-value pair, and as seen from your array, 'where' appears to be the key.
0

your $nav_items is a pretty mess, not a correct array.

$pageLoc = 'where';
$nav_items = array('index'=>'Home==>', 'where'=>'Where?==>', 'appeals'=>'Current Appeals==>', 'news'=>'Latest News==>', 'events'=>'Events==>', 'dontate'=>'Dontate==>');


foreach ($nav_items as $nav_class=>$nav_title) {
   if ($pageLoc == $nav_href) {
         echo '<li class="' . $nav_class .  'active">' . $nav_title . '</li>';
   } 
   else {
       echo '<li class="' . $nav_class . '">' . $nav_title . '</li>';
   }
}

Comments

0

There is no such syntax that makes it that easy. But here is what you probably want:

This is how you create a nested array which fits your needs:

$nav_items = array(
    array('href' => 'index',   'title' => 'Home',            'class' => ''), 
    array('href' => 'where',   'title' => 'Where?',          'class' => 'dropdown'), 
    array('href' => 'appeals', 'title' => 'Current Appeals', 'class' => ''), 
    array('href' => 'news',    'title' => 'Latest News',     'class' => ''), 
    array('href' => 'events',  'title' => 'Events',          'class' => ''), 
    array('href' => 'dontate', 'title' => 'Dontate',         'class' => ''),                
);

According to the above array structure this would be the foreach for you to use:

foreach ($nav_items as $item) {
    if ($pageHref == $item['href']) {
        echo '<li class="' . $item['class'] .  ' active">' . $item['title'] . '</li>';
    } else {
        echo '<li class="' . $item['class'] . '">' . $item['title'] . '</li>';
    }
}

DEMO FIDDLE

Here is 2 more notes:

Note1: In you could work with objects and create a nav item class containing the information which is above only the simple array('href' => 'dontate', 'title' => 'Dontate', 'class' => '')

Note2: Your line

echo '<li class="' . $nav_class .  'active">' . $nav_title . '</li>';

will concatenate both classes to one. So this code would give you dropdownactive as the class. You need to insert a space before active, else CSS won't recognize both classes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.