Welcome Guest, Not a member yet? Register   Sign In
The making of a menu with two css-classes.
#1

[eluser]überfuzz[/eluser]
I'm in the middle of mustering a one level menu. The menu is going to have two different css-classes. 'menu' and 'active' added if the url is right. The array used to render the array, an example:

Code:
Array
(
    [0] => Array
        (
            [segment] => home
            [name] => Home
        )

    [1] => Array
        (
            [segment] => agenda
            [name] => Agenda
        )

    [2] => Array
        (
            [segment] => contact
            [name] => Contact
        )

    [3] => Array
        (
            [segment] => etc
            [name] => Etc
        )

)
I'm looping through the array in a view-file to get the html code.
Code:
<div id="menu">
    <ul>
        &lt;?php
foreach($menu AS $value)
{
    $class = ($this->uri->segment(1) == $value['segment']) ? 'menu active' : 'menu';      //Nu är active = "menu active" och normal = "menu", ändra i fall du vill ha det på något annat sätt.
    echo "\n\t\t<li>";
    echo "<a >".$value['name']."</a>";
    echo "</li>";
}
?&gt;

    </ul>
</div>

I would appreciate some pointers on this matter. For instance, is there a nicer CI-way of doing this? How do you render your menus..?

Edit, the a href="" part isn't displayed properly..?
#2

[eluser]Aken[/eluser]
There's no CI way of doing this. Your method is just fine.

You can use an alternate syntax for the foreach() command if it helps use HTML around it:

Code:
<div id="menu">
    <ul>
        &lt;?php foreach ($menu AS $value): ?&gt;
        <li>
        &lt;?php $class = ($this->uri->segment(1) == $value['segment']) ? 'menu active' : 'menu'; //Nu är active = "menu active" och normal = "menu", ändra i fall du vill ha det på något annat sätt. ?&gt;
        <a href="#" class="&lt;?php echo $class; ?&gt;">&lt;?php echo $value['name']; ?&gt;</a>
        </li>
        &lt;?php endforeach; ?&gt;
    </ul>
</div>
#3

[eluser]überfuzz[/eluser]
Thats nifty. Tnx!




Theme © iAndrew 2016 - Forum software by © MyBB