1

I have this code here:

<ul>
<li<?php if ($page == 'home') echo ' class="active' ?>><a href="index.php">Home</a></li>
<li<?php if ($page == 'contact') echo ' class="active' ?>><a href="pages/contact.php">Contact Us</a></li>
<li<?php if ($page == 'services') echo ' class="active' ?>><a href="pages/services.php">Services</a></li>
<li<?php if ($page == 'employees') echo ' class="active' ?>><a href="pages/employees.php">Employees</a></li>
<li<?php if ($page == 'dashboard') echo ' class="active' ?>><a href="pages/dashboard.php">Dashboard</a></li>
</ul>

But I would like to refactor it to pull out some of that repetition. I am working with arrays, and have it somewhere like this:

$pages = array(
    "index.php" => "Home", 
    "contact.php" => "Contact Us", 
    "services.php" => "services", 
    "employees.php" => "Employees", 
    "dashboard.php" => "Dashboard");



foreach ($pages as $item) {
    if (isset($_GET['page']) && $_GET['page'] == $item) {
        echo 'class="active" ?><a href="' . array_keys($item) . '"> ' . array_values($item) . '</a></li>';
    } else {
        echo '<a href="' . array_keys($item) . '"> " . ' . array_values($item) . '</a></li>';}

I can't seem to make it work though. I want it to put the final line together for me add an active class to my nav bar tags when on that page and not otherwise. Is there a reasonable way to do this, and is it good practice?

1
  • How about using <?php foreach (...):?> and <?php endforeach; ?>? Commented Feb 23, 2014 at 5:30

3 Answers 3

1

There are a few things that aren't quite right:

  • That isn't how foreach works, $item will just be the value. It does not get set to an array on which you can use array_keys or array_values. You need foreach ($pages as $url => $label

  • You're missing the <li part of each line.

  • You have an extra ?> in your first echo, it should be a >.

  • You're duplicating a lot. The only thing that needs to be conditionally output is the `class="active"' part

foreach ($pages as $url => $label) {
  echo '<li ';

  if (isset($_GET['page']) && $_GET['page'] == $url) {
    echo 'class="active"';
  }
  echo '><a href="' . $url . '"> " . ' . $label. '</a></li>';
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try:

foreach ($pages as $item=>$val) {
    echo '<li';
    if (isset($_GET['page']) && $_GET['page'] == $item) {
        echo ' class="active" ><a href="' . $item . '"> ' . $val . '</a></li>';
    } else {
        echo '><a href="' . $item . '"> ' . $val . '</a></li>';}
}

You have quite some unneeded . and ". Removed those and you don't need to use array_keys or array_values if you are using foreach.

Comments

0
<ul>
<?php foreach ($pages as $key => $value): ?>
<?php 
$class = isset($_GET['page']) && $_GET['page'] === $key ? ' class="active"' : '';
?>
<li<?=$class?>><a href="<?=$key?>"><?=$value?></a></li>
<?php endforeach; ?>
</ul>

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.