Home › Forums › Languages › PHP & MySQL › Highlight current page menu item with PHP & dynamic title tag
Tagged: php
This topic contains 3 replies, has 2 voices, and was last updated by cinnamonsui 1 year ago.
-
AuthorPosts
-
April 25, 2012 at 10:25 pm #14036
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?
April 25, 2012 at 11:57 pm #15110
Anonymous1st 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.
April 26, 2012 at 12:07 am #15111
AnonymousAlso 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 ais 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_itemApril 26, 2012 at 7:00 pm #15112Yeah 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!
-
AuthorPosts
You must be logged in to reply to this topic.



Recent Comments