Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 09-16-2007, 07:21 AM   #1
Zayne E.
Confirmed User
 
Industry Role:
Join Date: Apr 2002
Posts: 1,383
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>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'how.php' ) { echo '<u>how it works</u>'; } else { echo '<a href="how.php">how it works</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'about.php' ) { echo '<u>about</u>'; } else { echo '<a href="about.php">about us</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'services.php' ) { echo '<u>services</u>'; } else { echo '<a href="services.php">services</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'benefits.php' ) { echo '<u>benefits</u>'; } else { echo '<a href="benefits.php">benefits</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'contact.php' ) { echo '<u>contact</u>'; } else { echo '<a href="contact.php">contact</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?php if ( $active == 'legal.php' ) { echo '<u>user agreement</u>'; } else { echo '<a href="legal.php">user agreement</a>'; } ?> &nbsp; &nbsp; | &nbsp; &nbsp;
  <?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!
Zayne E. is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 07:36 AM   #2
Linguist
Confirmed User
 
Join Date: Apr 2004
Location: Toronto, ON
Posts: 1,706
I used pretty much the same thing in my latest project:

PHP Code:
<li><a <? if ($web=="/" || $web == "/index.php") echo 'id="current"'; ?> href="index.php">home</a></li>
<li><a <? if ($web=="/products.php") echo 'id="current"'; ?> href="products.php">products</a></li>
<li><a <? if ($web == "/contact.php") echo 'id="current"'; ?> href="contact.php">contact</a></li>
Seems to do the job, I'd love to hear if anyone did it better though but I don't think you can improve it much
__________________
315-310
Linguist is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 07:39 AM   #3
J.P.
Confirmed User
 
Join Date: Jun 2004
Posts: 689
It would look a little less messy if you created a function for it...
__________________
Webmasters! Looking for new affiliate programs to promote?
Affiliate Program Search <-- Search for programs with FHGs, RSS feed, specific niche sponsors, ...
J.P. is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 07:39 AM   #4
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
try
Code:
$links['index.php'] = 'home';
$links['how.php'] = 'how it works';
$links['about'] = 'about us blah blah';

foreach($links as $link => $textLink) echo '<a href="'.$link.'"'.(($_SERVER['REQUEST_URI']==$link)?' class="active"':'').'>'.$textLink.'</a>'
Obviously you can tweak the array to waht you need, and also the output line, but it's simple as.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 07:41 AM   #5
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
Incidentally, it still is a rather nasty way of doing stuff - html and php together == no no.
You really should seperate code from formatting.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 08:28 AM   #6
AcidMax
Confirmed User
 
Join Date: May 2002
Location: MI
Posts: 1,827
Quote:
Originally Posted by quantum-x View Post
Incidentally, it still is a rather nasty way of doing stuff - html and php together == no no.
You really should seperate code from formatting.
What he is doing is not really code so to speak, its just templating. It's not any different if he was using smarty or any other templating system to determine what to show. Separating model/controller from views is more or less keeping actual logic out of the view.

Anyhow another way that can be done in a shorter syntax is:
Code:
<?=($active == 'content')?'id="content":''?>
__________________
Latest MMA news. http://www.mmawrapup.com

Last edited by AcidMax; 09-16-2007 at 08:30 AM..
AcidMax is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 09:35 AM   #7
MickeyG
Confirmed User
 
Join Date: May 2004
Location: South Florida
Posts: 4,134
I would use style sheets, and then use a SWITCH statement to turn id's off and on
MickeyG is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 10:16 AM   #8
allofzen
Confirmed User
 
Join Date: Jul 2007
Posts: 182
Quote:
Originally Posted by MickeyG View Post
I would use style sheets, and then use a SWITCH statement to turn id's off and on
Any examples of this? Sounds interesting.
__________________
www.adultsexzen.com - TGP
www.adultsexzen.com - TRADE TRAFFIC

Moniker - Your domains make you money - keep them safe! Search GFY for yourself!

JaugarPC HOSTING - reliable VPS HOSTING, 30 day MONEY BACK trial, TRY RISK FREE NOW!
allofzen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 10:40 AM   #9
rowan
Too lazy to set a custom title
 
Join Date: Mar 2002
Location: Australia
Posts: 17,393
Just a quick note, REQUEST_URI always starts with a "/" . if ( $active == 'how.php' ) will never match.
rowan is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 10:55 AM   #10
NiteRain
Confirmed User
 
Join Date: Jul 2002
Location: Fort Lauderdale
Posts: 600
Nothing is wrong with having presentation logic in your html, as long as you don't have your business logic there. However if you want your HTML a little cleaner, you can do this with stylesheets?

<link rel="stylesheet" href="highlight-style-css.php?<?= $_REQUEST['current'] ?>" type="text/css" />

Then you have have your menu like this:

<table>
<tr><td><a class="menu" id="home" href="blah">Home</a></td><td><a class="menu" id="link-codes" href="blah-links">Link Codes</a></td></tr>
</table>

In the style sheets, you have a default for menu, and the only one you override is the one you want to show as active.
__________________
AIM: PerlScriptor
NiteRain is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 11:00 AM   #11
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
Quote:
Originally Posted by allofzen View Post
Any examples of this? Sounds interesting.
code bloat.


Thelong and the short is, you can do this in one line of php, but it still won't be CLEAN design.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 11:21 AM   #12
Zayne E.
Confirmed User
 
Industry Role:
Join Date: Apr 2002
Posts: 1,383
Quote:
Originally Posted by J.P. View Post
It would look a little less messy if you created a function for it...
I have never written a function that works LOL
Zayne E. is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 11:22 AM   #13
Zayne E.
Confirmed User
 
Industry Role:
Join Date: Apr 2002
Posts: 1,383
Quote:
Originally Posted by quantum-x View Post
try
Code:
$links['index.php'] = 'home';
$links['how.php'] = 'how it works';
$links['about'] = 'about us blah blah';

foreach($links as $link => $textLink) echo '<a href="'.$link.'"'.(($_SERVER['REQUEST_URI']==$link)?' class="active"':'').'>'.$textLink.'</a>'
Obviously you can tweak the array to waht you need, and also the output line, but it's simple as.
This works very nicely for the top nav...couldn't figure out how to make it work for what I was trying to accomplish on the bottom nav but very nice...thanks
Zayne E. is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 11:33 AM   #14
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
Quote:
Originally Posted by Zayne E. View Post
This works very nicely for the top nav...couldn't figure out how to make it work for what I was trying to accomplish on the bottom nav but very nice...thanks
Just change the formatting you want in / not in for the line.

You don't have to repeat the array declaration.
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 09-16-2007, 01:28 PM   #15
AcidMax
Confirmed User
 
Join Date: May 2002
Location: MI
Posts: 1,827
Quote:
Originally Posted by quantum-x View Post
code bloat.


Thelong and the short is, you can do this in one line of php, but it still won't be CLEAN design.
This is why I use the method I spoke about. This way its no different than a template variable. Most WYSIWYG editors can view it without any issue, and your designers will be happy. It's simple, it's clean and it works efficiently. Too maybe { }'s, foreach's, if/else statements are just going to screw with the design when it comes time for you or your designer to play with the html. Keep it simple and it will work faster and be easier to manage.
__________________
Latest MMA news. http://www.mmawrapup.com
AcidMax is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.