Jump to content

PHP Include Problem


TomBullock

Recommended Posts

Hi

 

Ive only just started using PHP so go easy :)

 

I have a PHP include for a menu on a site I run.

 

<body>
<html>

<ul class="avmenu">

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

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

<li><a href="services.php">Services</a></li>

<li><a href="projects.php">Projects</a>

<ul>

<li><a href="highcroft_house.php">Highcroft House</a></li>

<li><a href="barn_conversion.php">Barn Conversion</a></li>

<li><a href="sunroom_extension.php">Sunroom Extension</a></li>

<li><a href="dance_school.php">Dance School</a></li>

</ul>

</li>
<li><a href="testimonials.php">Testimonials</a></li>

<li><a href="offers.php">Special Offers</a></li>

<li><a href="links.php">Links</a></li>

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

</ul>

 

As you can see, on the first link, the index link, it has the class="current" property which displays a little tab next to the menu to show what link is currently selected.

With using the include, obviously the class="current" is passed on to every page.

Is there a way that I can tell the include file to change according to what page its on?

 

Any help appreciated!

 

Thanks

Tom

Link to comment
Share on other sites

From a very simple standpoint, in each of your pages just set some variable for the tab number for that page and hit the include:

<?php 
$tab_no=1;
include("yourmenu.php"); 
?>

 

Then in your menu write out the menu item class (or not) based on the tab number:

<?php 
if ($tab_no=1) {
print ("<a class='current' href='mylink1.php'>Link 1</a>");
} else {
print ("<a href='mylink1.php'>Link 1</a>");
};

if ($tab_no=2) {
print ("<a class='current' href='mylink2.php'>Link 2</a>");
} else {
print ("<a href='mylink1.php'>Link 2</a>");
};

if ($tab_no=3) {
print ("<a class='current' href='mylink3.php'>Link 3</a>");
} else {
print ("<a href='mylink1.php'>Link 3</a>");
};

?>

 

Pretty rough (and can be simplified greatly), but does that point you in some direction you can follow?

 

J

Link to comment
Share on other sites

Off the top of my head, maybe:

 

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

 

Then something like this for every link:

 

<?php $class = ($page == 'index.php') ? 'current' : '';
<li><a class="<?php echo $class; ?>" href="index.php">Home</a></li>

 

Ahh, thank you! Worked a treat!

Link to comment
Share on other sites

Another issue that has arrised (nothing to do with the above code) is with a footer include.

 

The include code is:

 

<body>
<html>
<p>
Copyright © 2010 <a href="http://www.company.co.uk">Company</a> · Design by <a href="http://www.designer.co.uk">Designer</a></p>

 

When I view this in a browser, it gives me:

 

Copyright © 2010 Company · Design by Designer

 

I remember reading before that I need to put something before the © and the - but Im not sure if thats the correct way?

Link to comment
Share on other sites

 

Awesome! Ill keep a note of that site!

 

Another, more manageable way of doing this is to use an array containing the nav items. Then loop through to produce the html. That way, when you add new items you need only update the array, and not copy/paste code all over the place.

 

Thanks! Ill give it a go :)

Link to comment
Share on other sites

  • 9 months later...

As a follow up to this, I tried to use the same code method on another site but the class="current" won't work.

Here is the code:

 

<body>
<html>

<?php $page = basename($_SERVER['PHP_SELF']);?>
  
<div class="menu_nav">

<ul>

<?php $class = ($page == 'index.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="index.php">Home</a></li>
	  
<?php $class = ($page == 'facilities.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="facilities.php">Facilities</a></li>
	  
<?php $class = ($page == 'activities.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="activities.php">Activities</a></li>
	  
<?php $class = ($page == 'ssp.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="ssp.php">SSP</a></li>
	  
<?php $class = ($page == 'bookings.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="bookings.php">Bookings</a></li>
	  
<?php $class = ($page == 'contacts.php') ? 'active' : '';?>
<li><a class"<?php echo $class; ?>" href="contacts.php">Contacts</a></li>

  
</ul>

 

One thing I did notice is that before adding the inlcude to where the menu would go, the class was set to class="active". I did try changing the all the "current" in the code to "active" but with no joy?

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.