PHP Gurus...is there a better way?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zayne E.
    Confirmed User
    • Apr 2002
    • 1383

    #1

    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
    Confirmed User
    • Apr 2004
    • 1706

    #2
    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

    Comment

    • J.P.
      Confirmed User
      • Jun 2004
      • 689

      #3
      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, ...

      Comment

      • quantum-x
        Confirmed User
        • Feb 2002
        • 6863

        #4
        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.
        PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

        Comment

        • quantum-x
          Confirmed User
          • Feb 2002
          • 6863

          #5
          Incidentally, it still is a rather nasty way of doing stuff - html and php together == no no.
          You really should seperate code from formatting.
          PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

          Comment

          • AcidMax
            Confirmed User
            • May 2002
            • 1827

            #6
            Originally posted by quantum-x
            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":''?>
            Last edited by AcidMax; 09-16-2007, 07:30 AM.
            Latest MMA news. http://www.mmawrapup.com

            Comment

            • MickeyG
              Confirmed User
              • May 2004
              • 4134

              #7
              I would use style sheets, and then use a SWITCH statement to turn id's off and on

              Comment

              • allofzen
                Confirmed User
                • Jul 2007
                • 182

                #8
                Originally posted by MickeyG
                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!

                Comment

                • rowan
                  Too lazy to set a custom title
                  • Mar 2002
                  • 17393

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

                  Comment

                  • NiteRain
                    Confirmed User
                    • Jul 2002
                    • 600

                    #10
                    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

                    Comment

                    • quantum-x
                      Confirmed User
                      • Feb 2002
                      • 6863

                      #11
                      Originally posted by allofzen
                      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.
                      PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                      Comment

                      • Zayne E.
                        Confirmed User
                        • Apr 2002
                        • 1383

                        #12
                        Originally posted by J.P.
                        It would look a little less messy if you created a function for it...
                        I have never written a function that works LOL

                        Comment

                        • Zayne E.
                          Confirmed User
                          • Apr 2002
                          • 1383

                          #13
                          Originally posted by quantum-x
                          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

                          Comment

                          • quantum-x
                            Confirmed User
                            • Feb 2002
                            • 6863

                            #14
                            Originally posted by Zayne E.
                            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.
                            PrettyInCash.com - BoozedGFs.com - TeenGFs.com - JizzGFs.com- MilfUploads.com -

                            Comment

                            • AcidMax
                              Confirmed User
                              • May 2002
                              • 1827

                              #15
                              Originally posted by quantum-x
                              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

                              Comment

                              Working...