Jump to content

Using PHP to apply a class


Dilb

Recommended Posts

Hi,

 

I'm new to php, and I'm struggling to find out how to use php to set a class based on certain criteria. Basically, I'm using a file called navigation.html to store my primary navigation links as a list, and I'm using php's include once to access this in my pages. Obviously this means that I only need to change the navigation once to change it on all pages.

 

Trouble is I need to highlight the current page so that as users navigate around they know where they are. Is there some way I can use php to apply a class or id to the navigation link that points to the current page?

 

Thanks in advance.

 

Link to comment
Share on other sites

s there some way I can use php to apply a class or id to the navigation link that points to the current page?

 

Yes.... can we see navigation.html? (By the way, you'll need to make this a php file).

Link to comment
Share on other sites

Hi there,

 

An example of one of my pages might be:

 

<body>

<h2>Navigation</h2>

<ul>

<?php include("navigation.html"); ?>

</ul>

</body>

 

The 'navigation.html' file would then just be individual list items, such as:

 

<li><a href="index.php">Home</a></li>

<li><a href="about.php">About</a></li>

<li><a href="contact.php">Contact</a></li>

 

(Where I've put the end of the anchor, this forum is including [/url])

 

So obviously the list items would be added to the unordered list on each page, so I only have to update the navigation once for the whole site. So, if I'm on the home page, how do I use php to add a class to the Home list item?

 

Hope that's all you need. Thanks!

 

Link to comment
Share on other sites

  • 2 weeks later...

If you pass along the page the current user is on to your navigation.html page (which you'll need to make a .php page) you can have a differant class on the current page. So you will include like:

<?php 
include("http://www.yoursite.com/navigation.php?curr_page=about"); //whoops - forgot you'll need to use an absolute path otherwise you cant sent along get data
?>

 

Then, in your navigation page:

<?php
$pages = array("home","about","contact");//put all the possible pages into an array - far easier
$curr_page = $_GET['curr_page'];
foreach($pages as $value){
echo "<li";
if($value == $curr_page){
echo " class='currentpage'";
}
echo "><a href='".$value.".php'>$value</a></li>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.