including dynamic navigation

Hello, I have a php file for my footer and for my main nav menu that I just include in each page.
[php]<?php include 'includes/menu.php'; ?>[/php] it works fine, but I decided to have an ‘active’ class for my menu to show the page im on. How would I go about putting logic to the menu.php file to tell it to change the tab color to active on the about page when im on the about page?

This is whats in the menu.php file right now.
[php]

About

[/php]

Hi,

I would use one of two methods, first method i would consider is find the page name and then use a simple if statement to change the class if the page is equal to it. second you could use dynamic css using content css and use get variables but if i was you i would just use the below:

$currentFile = $_SERVER[“PHP_SELF”];
$page = Explode(’/’, $currentFile);
$currentpage = $page[count($page) - 1];

then for example use:

class="<?php if($currentpage == “test.php”){echo ‘active’;}>?"

Thank you for taking the time to help me, would you mind explaining the code you offered? I would like to learn how it works so I can actually learn a bit more about the language and actual logic.

[php]

    <?php
		if ($pageid == 1) { ?>

		<li class="current_page_item"><a href="#" class="first">Homepage</a></li>
        <?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>
		<li><a href="about.php">About Us</a></li>
        <li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
        <?php } ?>
        
		<?php } else if ($pageid == 2) { ?>
        
        <li><a href="index.php">Homepage</a></li>
		<li class="current_page_item"><a href="#" class="first">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <li><a href="about.php">About Us</a></li>
        <li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
        <?php } ?>
        
        <?php } else if ($pageid == 3) { session_start();?>
		
		<li><a href="index.php">Homepage</a></li>
		<li><a href="register.php">Join</a></li>
        <li class="current_page_item"><a href="#" class="first">Login</a></li>
		<li><a href="about.php">About Us</a></li>
        <li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
        <?php } ?>
        
        <?php } else if ($pageid == 4) {?>
        
        <li><a href="index.php">Homepage</a></li>
		<?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>			
        <li class="current_page_item"><a href="#" class="first">About Us</a></li>
        <li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
		<?php } ?>
        
        <?php } else if ($pageid == 5) { ?>
			
		<li><a href="index.php">Homepage</a></li>
        <?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>			
        <li><a href="about.php">About Us</a></li>
        <li><a href="faq.php">FAQs</a></li>
		<li class="current_page_item"><a href="#" class="first">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
		<?php } ?>
        
        <?php } else if ($pageid == 6) { session_start();?>
			
		<li><a href="index.php">Homepage</a></li>
    	<?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>			
        <li><a href="about.php">About Us</a></li>
       	<li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <li class="current_page_item"><a href="#" class="first">My Account</a></li>
       
        <?php } else if ($pageid == 7) { ?>
			
		<li><a href="index.php">Homepage</a></li>
        <?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>
		<li><a href="about.php">About Us</a></li>
		<li class="current_page_item"><a href="faq.php" class="first">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
        <?php } ?>
        
		<?php } else { 
		
		if ($pageid != "ses"){session_start();}
		?>	
		<li><a href="index.php">Homepage</a></li>
        <?php if (!isset($_SESSION['user_id'])) { ?>
		<li><a href="register.php">Join</a></li>
        <li><a href="login.php">Login</a></li>
        <?php } ?>			
        <li><a href="about.php">About Us</a></li>
       	<li><a href="faq.php">FAQs</a></li>
		<li><a href="contact.php">Contact Us</a></li>
        <?php if (isset($_SESSION['user_id'])) { ?>
        <li><a href="myaccount.php">My Account</a></li>
        <?php } ?>
        <?php }
		?>
			
	</ul>

[/php]

Thats how I got about the issue… It was long winded and requires you to have each page with an ID of its own. ie Declare at the top of the page: $pageid == 1 for the home page etc

Rather than code this in PHP why not just have a definition in your CSS for a:active?

Have a look at http://www.w3schools.com/cssref/sel_active.asp

Basically the current file is your current page, the the explode seperates the page into sections but uses the last section
For example it may leave you with index.php but it you are using get variables and you don’t want them to show add a following line $currentpage = explode(’?’,$currentpage); $currentpage= $currentpage[0];
This will remove all the get variables for you. Sorry for the poor description but hope it helps

Sponsor our Newsletter | Privacy Policy | Terms of Service