Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

probably easy php question

  • 02-05-2009 07:25AM
    #1
    Registered Users, Registered Users 2 Posts: 872 ✭✭✭


    Hi,

    I am making a simple site and want to use php includes for the common elements (header,footer etc)

    I also want a navigation include that i can pass a parameter into so it knows what link the active css class should be applied to ..

    i.e. if i am on the about us page i want to include the navigation file but tell it to apply the active class to the about us link.

    I am sure there is an easy way to do this but im a bit of a novice with php :D

    Thanks in advance


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Well you could base it on the file name:

    [php]

    $currentFile = strtolower(basename($_SERVER));

    printf("<a class='%s'>About us</a>", $currentFile == "aboutus.php" ? "aboutClass" : "defaultLink" );
    [/php]


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    Similar idea to Webmonkey.

    In my html I have something like:
    [PHP]<html>
    <head>
    <?php include( 'common.php' ); ?>
    </head>
    <body>
    <?php include_menu(); ?>
    </body>
    [/PHP]
    And common.php has:
    [PHP]function include_menu() {
    $menu = array (
    'index.php' => array( 'Home', 'Go back to the home page' ),
    'services.php' => array( 'Services', 'See the list of services we provide' ),
    'portfolio.php' => array( 'Portfolio', 'See photos of our work' ),
    'contact-us.php' => array( 'Contact Us', 'Get in contact with us' ),
    );

    echo '<div id="menu">', "\n";
    foreach ($menu as $page => $info) {
    if ( strpos( $_SERVER, $page ) !== false ) {
    $link_class = 'class="active"';
    } else {
    $link_class = '';
    }
    printf( '<a %s href="/%s" title="%s">%s</a>%s', $link_class, $page, $info[1], $info[0], "\n" );
    }
    echo '</div>', "\n";
    }
    [/PHP]
    class="active" indicates the active page.


  • Closed Accounts Posts: 176 ✭✭elyod


    [php]
    printf( '<a %s href="/%s" title="%s">%s</a>%s', $link_class, $page, $info[1], $info[0], "\n" );
    [/php]

    Just out of interest, is there any particular advantage using printf() over something like
    [php]
    echo "<a $link_class href='$page' title='$info[1]'>$info[0]</a>\n";
    [/php]

    Obviously a better syntax highlighter would colour the variables to make them stand out.


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    elyod wrote: »
    [php]
    printf( '<a %s href="/%s" title="%s">%s</a>%s', $link_class, $page, $info[1], $info[0], "\n" );
    [/php]

    Just out of interest, is there any particular advantage using printf() over something like
    [php]
    echo "<a $link_class href='$page' title='$info[1]'>$info[0]</a>\n";
    [/php]

    Obviously a better syntax highlighter would colour the variables to make them stand out.
    I program in C a lot of hence why I use printf. Whatever you want yourself but I can ready the printf's easier.


  • Registered Users, Registered Users 2 Posts: 6,652 ✭✭✭daymobrew


    Apparently there is a minute speed difference (with echo being a bit faster).
    I used printf to make the code easier to read - it keeps the html code together.


  • Advertisement
  • Closed Accounts Posts: 275 ✭✭Hydrosylator


    I've used similar code to the array checks above in a few sites.
    However, if you want to avoid having to execute php script for the style every time, it can be done with css as well, using a unique div for each page.

    So, your contact page would be in a unique div called contact, and so on.
    Each link would be given a unique class or id as well, like "contact_link" or whatever

    Then your css would be something like

    .link{
    (regular link css)
    }

    #home .home_link,
    #contact .contact_link,
    #about .about_link{
    (active link css)
    }

    This way might be slightly quicker for loading pages, but you might not notice a significant difference.

    It's better for sites where the content doesn't change much. If you go adding new links to the nav you have to add new code to the css, as well as the right div to the new page.


Advertisement