Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

probably easy php question

Options
  • 02-05-2009 7:25am
    #1
    Registered Users 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 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 Posts: 6,511 ✭✭✭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 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 Posts: 6,511 ✭✭✭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