1

I've never used the php include function but I recently read some good articles which encourage ti. I'm going to use it mainly for menus and footers, but here's my question:

How can I attach a HTML class to a menu element.

e.g.

menu.php

<nav>
    <ul>
        <li><a href=""></a>home</li>
        <li><a href=""></a>blog</li>
        <li><a href=""></a>about</li>
        <li><a href=""></a>contact</li>
    </ul>
</nav>

index.html

<?php include 'menu.php'; ?>

In this case how do I set a class to any of these menu elemnts without using different menu.php files?

Long story short I need it so I can apply certain styles to a single element in the list, and each html file will match the specific menu voice

E. G. index.html will have the "home" anchor styled differently than other three elements.

Thanks in advance.

oil-gap
  • 37
  • 1
  • 1
  • 6
  • 1
    Sorry, it is unlear what you actually ask for and what you try to do. Please _edit_ your questions text above using the `edit` button) and add more details. Try to think from the readers point of view... – arkascha Aug 03 '13 at 17:32
  • What do you mean exactly by *"How can I attach a class to a menu element."*? Do you mean a CSS class, or a PHP class? You need to be more specific. PHP classes themselves are set inside `included` files, as an example, if that's what you're looking to do. More on PHP classes here: http://php.net/manual/en/language.oop5.basic.php **and** CSS classes: http://www.tizag.com/cssT/class.php – Funk Forty Niner Aug 03 '13 at 18:22
  • I edited the question, I hope now it's more clear!... Thanks – oil-gap Aug 03 '13 at 18:39
  • @oil-gap You can use `
  • ...
  • ` and your CSS will look something like this `li.my-class {....}` – Funk Forty Niner Aug 03 '13 at 18:59
  • @oil-gap Have a look at this answer on SO, will surely help http://stackoverflow.com/a/4983739/1415724 – Funk Forty Niner Aug 03 '13 at 19:01
  • @oil-gap You can also use `
      `. There is also `current` which can highlight the current element if needed.
    – Funk Forty Niner Aug 03 '13 at 19:03
  • @Fred I think I ve been misunderstood here. I do know how to add a class to an element, this is not the problem. I'll try to be more coear: I have a menu which is inside the menu.php file ok? Now I want to inject this menu in every html page via the include function. Let's say that i'm in the home page (index.html). I want the element
  • home
  • to be colored differently from the other ones, so the user can better understand where he's. I want to achieve this by adding a class to the element, but since it is included in the menu.php file I can't. – oil-gap Aug 03 '13 at 21:16
  • @oil-gap Have a look at this answer on SO, tell me if that's what you're looking for http://stackoverflow.com/a/10776404/1415724 – Funk Forty Niner Aug 04 '13 at 02:02
  • @oil-gap Actually, there's nothing stopping you that's stopping you from styling directly inside your `menu.php` file. You can also do `inline` CSS. One thing to remember though is, if you have any output "above" PHP using `header("Location: file.php");` happening, you'll get an error. Have you tried this? – Funk Forty Niner Aug 04 '13 at 02:38
  • @Fred Thank you for the answer. Ok, I could set the CSS class "active" directly inside my menu.php file but in this way I'd have to create one file for every menu voice and the use of include would become unnecessary. I would end with *home.php* *blog.php* *about.php* and *contact.php*. I never use inline styles, I only use classes or IDs. – oil-gap Aug 04 '13 at 13:59
  • @oil-gap I don't know how to do it with just the one file. This is the best example I could find, if it's what I'm thinking, but I'm having a real hard time wrapping my head around this. Have a look at this link http://blog.elanman.com/2009/01/highlight-current-page-using-php-includes/ – Funk Forty Niner Aug 04 '13 at 14:37
  • @oil-gap Found this too http://www.websitecodetutorials.com/code/css/highlight-current-page.php – Funk Forty Niner Aug 04 '13 at 15:02
  • @oil-gap Also look at example #5 on http://forums.phpfreaks.com/topic/232968-setting-the-style-on-the-current-page/ **and** http://www.evowebdev.com/currentnavdemo.php .ZIP is on that page also. And http://www.yourinspirationweb.com/en/how-to-build-a-dynamic-and-maintainable-navigation/ – Funk Forty Niner Aug 04 '13 at 15:14
  • @oil-gap I've Google'd what I could for you. I hope the links I've given you will help you to achieve your goal. Keep me posted. – Funk Forty Niner Aug 04 '13 at 15:23
  • @Fred thank you so much for your help! The link you posted has a real simply and clever solution to achieve what I was asking for! How could I leave you feedback?! – oil-gap Aug 04 '13 at 18:49
  • @oil-gap You're welcome. Which link was that? – Funk Forty Niner Aug 04 '13 at 23:11
  • @oil-gap I could use the link in question as an answer. Which link was it that you were referring to? – Funk Forty Niner Aug 05 '13 at 17:54
  • @Fred http://www.websitecodetutorials.com/code/css/highlight-current-page.php this was the link! The second opinion is my choice. Thanks again! – oil-gap Aug 05 '13 at 21:34
  • @oil-gap You're welcome, glad I could help. My answer is below, cheers. – Funk Forty Niner Aug 05 '13 at 21:44