Highlight current page menu item with PHP & dynamic title tag

Home Forums Languages PHP & MySQL Highlight current page menu item with PHP & dynamic title tag

Tagged: 

This topic contains 3 replies, has 2 voices, and was last updated by  cinnamonsui 1 year ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #14036

    cinnamonsui
    Participant

    I’ve looked at several examples but can’t get these 2 things to work (I’m a PHP-noob).

    Check this page: http://pixelit.se/webbinformatik/om.php

    the tab “Om oss” should be brown since it’s the current page, but it’s not working.

    I’m using this code:

    <li>
    <a <?php
    if (strpos($_SERVER['PHP_SELF'], 'om.php'))
    echo 'class="current_page_item"'; ?>
    href="om.php" title="Mer information om oss" tabindex="2">Om oss</a>
    </li>

    What’s wrong here?

    And I’m also trying to create a dynamic title-tag that should change depending on what page you’re on. This doesn’t work either:

    <?php
    $page = $_SERVER['PHP_SELF'];

    if (isset($page)) {

    switch($page) {

    case "index.php" :
    $title = "Hem";

    break;

    case "om.php" :
    $title = "Om oss";

    break;

    case "bakverk.php" :
    $title = "Våra bakverk";

    break;

    case "cupcakes.php" :
    $title = "Våra bakverk - Cupcakes";

    break;

    case "tartor.php" :
    $title = "Våra bakverk - Tårtor";

    break;

    case "bestallning.php" :
    $title = "Beställning";

    break;

    case "kontakt.php" :
    $title = "Kontakt";

    break;
    }
    } else {
    $title = "Hello Sugar Bakery";
    }

    print "<title>$title</title>";
    ?>

    Does anyone know what I’m doing wrong here? ;)

    #15110

    Anonymous

    1st one, your code actually works for me. Can you post the entire navigation code?

    2nd one: By changing

    $page = $_SERVER['PHP_SELF'];

    To

    $page = baseename($_SERVER['PHP_SELF']);

    It then works.

    #15111

    Anonymous

    Also your CSS is wrong if we get the php working. If we get your php working, the class current_page_item is being applied to the a tag, not the li tag. Thus your css you have where it says:

    .horizmenu ul li.current_page_item a, .horizmenu ul li.current_page_item ul li a

    is incorrect since you are applying the class current_page_item to the li tag. Change to

    .horizmenu ul li a.current_page_item, .horizmenu ul li ul li a.current_page_item

    #15112

    cinnamonsui
    Participant

    Yeah I noticed this morning that the CSS was all wrong. No wonder it didn’t work!

    And I also figured out why my second problem didn’t work, I had to add the folder name to the source path; case “webbinformatik/index.php”.

    But thanks for your help! :)

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.