/ Published in: PHP
Aimed at designers who want to sprinkle in a little PHP to make development easier. If building/prototyping a site with HTML/PHP, this can save time by constructing the navigation list on the fly, based on re-usable navigation settings.
Usage: Assuming you have multiple HTML/PHP pages, simply add $page definition to the top of each page, include the PHP files, then configure the settings to build your navigation. (See the detail of each of the three core pages below for more information.)
Usage: Assuming you have multiple HTML/PHP pages, simply add $page definition to the top of each page, include the PHP files, then configure the settings to build your navigation. (See the detail of each of the three core pages below for more information.)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php // Create (at least) 2 pages: // 1) page.php : this is the rendered html page // 2) nav-settings.php : this holds the settings of number of nav items, urls, etc. ?> <!-- page.php ---------------------------------------- --> <html> <head> <!-- what page am i? --> <?php global $page; $page="PAGE_SHORTNAME or STRING FOR TITLE"; // see nav-settings.php ?> <?php include('nav-settings.php'); ?> <!-- will write title tag: see nav-settings.php --> </head> <body id="<?php echo $page; ?>"> <!-- optional: used for css styling purposes if necessary --> <div> <!-- insert navigation list here --> <?php echo $navbarList; ?> <!-- will produce something like this (assuming $page='ordering'): --> <!-- <ul> <li><a href="#">Home</a></li> <li><a href="#">Products</a></li> <li class="selected"><span>Ordering Info</span></li> <li><a href="#">Press Room</a></li> <li><a href="#">Events</a></li> <li><a href="#">Publications</a></li> <li><a href="#">Business Development</a></li> </ul> --> </div> </body> </html> <!-- nav-settings.php ---------------------------------------- --> <?php /* declare global variables ---------------------------------------- */ global $tab; // shortname of tab/page global $tabName; // long (display) name of tab/page global $page; // name of page for <title> global $navbarTabs; // array holding tab and tabNames global $navbarURLs; // array holding tab URLs global $navbarList; // string holding the navBar // make sure these arrays match! // (there are more efficient ways of building these arrays, but I am shooting for simplicity) "home" => "Home", "products" => "Products", "ordering" => "Ordering Info", "press" => "Press Room", "events" => "Events", "publications" => "Publications", "contract" => "Business Development" ); "home" => '#', // urls for the links "products" => '#', "ordering" => '#', "press" => '#', "events" => '#', "publications" => '#', "contract" => '#' ); // check current page, then determine name of current page // if given a non-shortname value, $page can be the custom name of the page if ( ($page == "home") || ($page == "products") || ($page == "events") || ($page == "ordering") || ($page == "press") || ($page == "publications") || ($page == "contract")) { $tabName = $navbarTabs[$page]; } else { $tabName = $page; } // create navigation list => $navbarList (echo it whenever you need the nav list) $navbarList = "<ul>\n"; // add id or class if needed for styling foreach ($navbarTabs as $k => $v) { switch ($k) { case $page: $navbarList = $navbarList."<li class=\"selected\"><span>$v</span></li>\n"; break; default: $link = $navbarURLs[$k]; $navbarList = $navbarList."<li><a href=\"$link\">$v</a></li>\n"; } } $navbarList = $navbarList."</ul>"; // print title tag on HTML page echo '<title>SITE NAME : '.$tabName.'</title>'; ?>