GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Gurus...is there a better way? (https://gfy.com/showthread.php?t=769133)

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>'; } ?> &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!

Linguist 09-16-2007 07:36 AM

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

J.P. 09-16-2007 07:39 AM

It would look a little less messy if you created a function for it...

quantum-x 09-16-2007 07:39 AM

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 09-16-2007 07:41 AM

Incidentally, it still is a rather nasty way of doing stuff - html and php together == no no.
You really should seperate code from formatting.

AcidMax 09-16-2007 08:28 AM

Quote:

Originally Posted by quantum-x (Post 13094854)
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":''?>

MickeyG 09-16-2007 09:35 AM

I would use style sheets, and then use a SWITCH statement to turn id's off and on :2 cents:

allofzen 09-16-2007 10:16 AM

Quote:

Originally Posted by MickeyG (Post 13095100)
I would use style sheets, and then use a SWITCH statement to turn id's off and on :2 cents:

Any examples of this? Sounds interesting.

rowan 09-16-2007 10:40 AM

Just a quick note, REQUEST_URI always starts with a "/" . if ( $active == 'how.php' ) will never match.

NiteRain 09-16-2007 10:55 AM

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.

quantum-x 09-16-2007 11:00 AM

Quote:

Originally Posted by allofzen (Post 13095202)
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.

Zayne E. 09-16-2007 11:21 AM

Quote:

Originally Posted by J.P. (Post 13094847)
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. 09-16-2007 11:22 AM

Quote:

Originally Posted by quantum-x (Post 13094850)
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 :thumbsup

quantum-x 09-16-2007 11:33 AM

Quote:

Originally Posted by Zayne E. (Post 13095386)
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 :thumbsup

Just change the formatting you want in / not in for the line.

You don't have to repeat the array declaration.

AcidMax 09-16-2007 01:28 PM

Quote:

Originally Posted by quantum-x (Post 13095322)
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.


All times are GMT -7. The time now is 12:25 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123