javascript - How to include sidebar with php and use different color for current page in sidebar? - Stack Overflow

On my web site every page has the same sidebar which is just a link to every page. Instead of writing t

On my web site every page has the same sidebar which is just a link to every page. Instead of writing the same thing on every page I'm using a file 'sidebar.html' which is then included to every page with php:

<?php include 'sidebar.html'; ?>

Now I would like to have the name of the current page in sidebar in different colour than any other page name. I have done this with JavaScript like this: 1. get the current page name by the specific id 2. set the new id called 'active' to this name and in css #active gets a new colour

I'm wondering if there is any other solution to obtain the same result and maybe if there is a solution without using JavaScript.

Thank you!

On my web site every page has the same sidebar which is just a link to every page. Instead of writing the same thing on every page I'm using a file 'sidebar.html' which is then included to every page with php:

<?php include 'sidebar.html'; ?>

Now I would like to have the name of the current page in sidebar in different colour than any other page name. I have done this with JavaScript like this: 1. get the current page name by the specific id 2. set the new id called 'active' to this name and in css #active gets a new colour

I'm wondering if there is any other solution to obtain the same result and maybe if there is a solution without using JavaScript.

Thank you!

Share Improve this question asked Nov 18, 2013 at 15:48 user2923638user2923638 251 silver badge5 bronze badges 1
  • use a .php file inside, and have PHP generate a small inline <style> block that contains your random color. – Marc B Commented Nov 18, 2013 at 15:50
Add a ment  | 

4 Answers 4

Reset to default 4

You can replace your sidebar.html by a sidebar.php file, and simply use PHP variables ; for example, you can define in your current page a variable that represent the current ID, and use it inside your sidebar.php to determine which link should be active

The other answers suggesting a PHP variable is one way to do this. On the other hand you could do this without using PHP or JavaScript, just pure CSS.

Start by indicating the current page with a class name on your <body> tag or other high-level wrapper.

<body class="contact-page">

Then in your sidebar, make sure each link has a unique class as well. Something like:

<a href="/" class="sidebar-home">Home</a>
<a href="/contact.php" class="sidebar-contact">Contact</a>
... and so on

Now you can just write CSS classes to do the rest! So if you want your selected links to be blue for example...

.home-page .sidebar-home { color: blue; }
.contact-page .sidebar-contact { color: blue; }

You can change the html file to a php file and pass a parameter.

Then, in the new sidebar.php, get the parameter and define the active class accordingly.

I would do something like this:

sidebar.php

$links = array();
$links['page1.html'] = 'Page 1';
$links['page2.html'] = 'Page 2';
$links['page3.html'] = 'Page 3';

$curr_page = basename($_SERVER['PHP_SELF']);

foreach($links as $k=>$v)
{
    echo '<a href="'.$k.'"';
    if($curr_page === $k)
    {
        echo ' class="active_link"';
    }
    echo '>'.$v.'</a>';
}

Just wanted to point out that things will get plicated if you put files into sub-directories like this:

$links = array();
$links['subdir1/page1.html'] = 'Page 1';
$links['subdir2/page2.html'] = 'Page 2';
$links['subdir3/page3.html'] = 'Page 3';

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745421529a4626985.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信