HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Navigation item selection

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
itemnavigationselection

Problem

Using Bootstrap 3 and PHP, I create my navigation bars in the following way:


  
    
      
        
        
        
      
      Company Name
    
    
      
        >Link One
        >Link Two
        >Link Three
        >Link Four
      
    
  


Then, depending on which page you're on, a link within the navigation has class="active" applied to it. However, this feels like a very messy way of building this functionality to the ``. Lots of empty variables at one time, and a lot of almost identical code for each navigation element.

How best can I improve on this code?

Solution

Use an array and a function

You've got quite a few pieces of code that look very similar. The more menu items you have, the worse this gets. This is almost always a sign you should start using a function. Here's how to do that:

 'one.php',
                     'Link Two'   => 'two.php',
                     'Link Three' => 'three.php',
                     'Link Four'  => 'four.php');

  function navigationItem($title,$page)
  {
    $class = (strpos($_SERVER['REQUEST_URI'],$page) !== false) ? ' class="active"' : '';
    return '        '.$title.''.PHP_EOL;
  }

?>

  
    
      
        
        
        
      
      Company Name
    
    
      
 $page) echo navigationItem($title,$page);

?>      
    
  


As you can see I've put both lines, which you repeat four times, inside the function and simply call that function four time. I could have done that by simply writing:



but I prefer to have the menu at the top of the file in an array. This makes it easier to find and edit.

This was probably the next step you were looking for. But it should not be your last.

You could easily generalize the navigationItem() function for all list items you will make. And the foreach loop basically builds a list, why not make a function out of that?

After that you could make a class which could build a whole menu bar for you. The functions will become methods of that class.

Why not put the array in a seperate file with the other stuff that defines your site? Perhaps eventually create a database table with menu items?

There are many ways in which you can improve this tiny piece of code.

What you should definately NOT do is: Build a whole site with Bootstrap HTML code interspersed by piece of loose PHP code. That will be chaos, and very difficult to maintain and expand. Be systematic about it.

Preferably try to avoid putting piece of PHP code inside HTML pages. It's just so ugly. Personally I prefer to write in pure PHP, and generate the HTML using objects similar to the class I suggested for your menu bar.

Code Snippets

<?php

  $menuItems = array('Link One'   => 'one.php',
                     'Link Two'   => 'two.php',
                     'Link Three' => 'three.php',
                     'Link Four'  => 'four.php');

  function navigationItem($title,$page)
  {
    $class = (strpos($_SERVER['REQUEST_URI'],$page) !== false) ? ' class="active"' : '';
    return '        <li'.$class.'><a href="'.$page.'">'.$title.'</a></li>'.PHP_EOL;
  }

?>
<div class="navbar navbar-default">
  <div class="container">
    <div class="navbar-header">
      <button class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <i class="icon-bar"></i>
        <i class="icon-bar"></i>
        <i class="icon-bar"></i>
      </button>
      <a class="navbar-brand" href="index.php">Company Name</a>
    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav">
<?php

  foreach ($menuItems as $title => $page) echo navigationItem($title,$page);

?>      </ul>
    </div>
  </div>
</div>
<?php
  echo navigationItem('Link One','one.php');
  echo navigationItem('Link Two','two.php');
  echo navigationItem('Link Three','three.php');
  echo navigationItem('Link Four','four.php');
?>

Context

StackExchange Code Review Q#89980, answer score: 3

Revisions (0)

No revisions yet.