Zayne E. |
09-16-2007 07:21 AM |
PHP Gurus...is there a better way?
I use PHP a lot though I am about to prove my lack of proficiency. What I am trying to do is make a single header.php and footer.php to use throughout a site...but I want to be able to use link highlighting. So, for the footer, I am trying to do this:
PHP Code:
<?php if ( $active == '/' || $active == 'index.php' ) { echo '<u>home</u>'; } else { echo '<a href="/">home</a>'; } ?> |
<?php if ( $active == 'how.php' ) { echo '<u>how it works</u>'; } else { echo '<a href="how.php">how it works</a>'; } ?> |
<?php if ( $active == 'about.php' ) { echo '<u>about</u>'; } else { echo '<a href="about.php">about us</a>'; } ?> |
<?php if ( $active == 'services.php' ) { echo '<u>services</u>'; } else { echo '<a href="services.php">services</a>'; } ?> |
<?php if ( $active == 'benefits.php' ) { echo '<u>benefits</u>'; } else { echo '<a href="benefits.php">benefits</a>'; } ?> |
<?php if ( $active == 'contact.php' ) { echo '<u>contact</u>'; } else { echo '<a href="contact.php">contact</a>'; } ?> |
<?php if ( $active == 'legal.php' ) { echo '<u>user agreement</u>'; } else { echo '<a href="legal.php">user agreement</a>'; } ?> |
<?php if ( $active == 'privacy.php' ) { echo '<u>privacy policy</u>'; } else { echo '<a href="privacy.php">privacy policy</a>'; } ?>
Obviously, it's really chunky, clunky and ugly. And for the header I am trying to do similar with this:
PHP Code:
<ul>
<li<?php if ( $active == '/' || $active == 'index.php' ) { echo ' class="active"'; } ?>><a href="index.php"><span>Home</span></a></li>
<li<?php if ( $active == 'how.php' ) { echo ' class="active"'; } ?>><a href="how.php"><span>How it works</span></a></li>
<li<?php if ( $active == 'about.php' ) { echo ' class="active"'; } ?>><a href="about.php"><span>About us</span></a></li>
<li<?php if ( $active == 'services.php' ) { echo ' class="active"'; } ?>><a href="services.php"><span>Services</span></a></li>
<li<?php if ( $active == 'benefits.php' ) { echo ' class="active"'; } ?>><a href="benefits.php"><span>Benefits</span></a></li>
<li<?php if ( $active == 'contact.php' ) { echo ' class="active"'; } ?>><a href="contact.php"><span>Contact</span></a></li>
</ul>
Obviously, I heave this in the header already:
PHP Code:
$active = $_SERVER['REQUEST_URI'];
I want to streamline those 2 chunks of code (and the links are not being pulled from a database).
Help is appreciated!
|