Yousef Altaf
Yousef Altaf

Reputation: 2763

php adding class='active' to menu

I have a function called 'kmenu'

function kmenu()
{
    if (isset($_GET['pid'])) {
        $pid = $_GET['pid'];
    } else {
        $pid = 1;
    }


    $menu = '<ul class="nav navbar-nav">
                        <li class="active"><a href="?pid=1">HOME <span class="sr-only">(current)</span></a></li>
                        <li><a href="?pid=2">ABOUT US</a></li>
                        <li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                                aria-expanded="false">OUR PRODUCTS<span class="caret"></span></a>
                            <ul class="dropdown-menu" role="menu">
                                <li><a href="#">Arabic Sweets</a></li>
                                <li><a href="#">Cakes</a></li>
                                <li><a href="#">Bakery</a></li>
                                <li><a href="#">Chocolate</a></li>
                                <li><a href="#">Confectionery</a></li>
                                <li><a href="#">Ice Cream</a></li>
                                <li><a href="#">Malaga</a></li>
                            </ul>
                        </li>
                        <li><a href="#">OUR BRANCHES</a></li>
                        <li><a href="#">GALLERY</a></li>
                        <li><a href="#">CONTACT US</a></li>
                    </ul>';
    return $menu;
}

this function content all the menu items as shown above. I need to add the class='active' to the current page. I used to make it like this before if($pid==1){echo 'class="active"'} but when I try to use functions to store the menu how can I do this now.

Upvotes: 2

Views: 2117

Answers (3)

Jacek Kowalewski
Jacek Kowalewski

Reputation: 2851

OK. At first You can do something like this:

$menu = 'MENU';
$menu = $menu.'SOMETHINGELSE';
print_r($menu); // or return $menu;

In that case it will print / return "MENUSOMETHINGELSE". This is string concatenation. So You can do simple if (as You said you make it before), and write:

$menu = '';
$menu .= 'BEGINNING OF THE HTML';
if($pid==1) {
    $menu .= 'class="active"';
}
$menu .= 'REST OF HTML';

Few more words: But this is not everything. Storing menu in a variable looks really bad. Also outputing HTML like that too. You should try to save only important value in array, and then return / echo menu in loop. For example:

$menu = array(
    [0] => array('name' => 'NAME0', 'title' => 'TITLE0', 'url' = '/url0'), 
    [1] => array('name' => 'NAME1', 'title' => 'TITLE1', 'url' = '/url1')
);

Then generate menu:

foreach ($menu as $m) {
    echo '<a href="'.$m['url'].'" title="'.$m['title'].'">'.$m['name'].'</a>';
}

In that case, visual settings are in foreach loop, and menu semantic in simple array. You can store active in that array too.

If You want, You can also take a look at ob_start functions, and use echo, and then return ob_get_contents(), if this is easier for You (http://php.net/manual/en/ref.outcontrol.php).


Remember that on every fragment of PHP code You can escape php.

<?php echo "ASD"; ?>
ASD
<?php echo "ASD"; ?>

This code will output three ASD. You should try to use this technique, and minimize echo ouput of Your application. You can also use it in loops:

<?php foreach ($menu as $m) { ?>
    <a href="<?php echo $m['url']; ?>" 
       title="<?php echo $m['title']; ?>">
        <?php echo $m['name']; ?>
    </a>
<?php } ?>

I hope You get the idea. If there is much more constant html (as in Your example) this should be much more cleaner to output.


Another thing is storing the menu content inside a function. It is also not a good practice. You should try to switch to object programming style, and try to save this menu array in some sort of global / cache / another object.

In Your case, if someone needs to create two menus on the same site, each time he must call this function, and the whole menu variable is declared from the beginning. While it should be only accessible from the function.

If You dont need to make an object oriented application, or need an easier solution, it is much better to save $menu somewhere else (level higher), and pass it as an argument to for example menu_generator function.

In that case, $menu look like I described it, and You declare function like that:

function generate_menu_html($menu) {
    // your menu generation function base on menu array
}

Hope it helps.

Best regards.

Upvotes: 3

Dieter Pollier
Dieter Pollier

Reputation: 839

I think you shouldn't store html-code in a function. Normally you store these in seperate files and then inlude them. for example:

require '/menu.php'

Then you would be able to add to each line

<li><?php if($pid == 1): ?> class='active'<?php endif; ?><a>...</a></li>

Upvotes: 1

Rupam
Rupam

Reputation: 1642

You can try something like this

$menu = '<ul class="nav navbar-nav">';
$menu .= '<li';
$menu .= ($pid==1)?' class="active"':'';
$menu .= '><a href="?pid=1">HOME ....REST OF THE CODES';

Upvotes: 0

Related Questions