Return to Snippet

Revision: 23969
at February 24, 2010 16:45 by rareyman


Updated Code
<?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)

$navbarTabs = array(
        "home"          =>      "Home",
        "products"      =>      "Products",
        "ordering"      =>      "Ordering Info",
        "press"         =>      "Press Room",
        "events"        =>      "Events",
        "publications"  =>      "Publications",
        "contract"      =>      "Business Development"
);

$navbarURLs = array(
        "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>';

?>

Revision: 23968
at February 24, 2010 15:55 by rareyman


Updated Code
<?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)

$navbarTabs = array(
        "home"          =>      "Home",
        "products"      =>      "Products",
        "ordering"      =>      "Ordering Info",
        "press"         =>      "Press Room",
        "events"        =>      "Events",
        "publications"  =>      "Publications",
        "contract"      =>      "Business Development"
);

$navbarTopURLs = array(
        "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>';

?>

Revision: 23967
at February 17, 2010 18:54 by rareyman


Initial Code
<?php
// Create 3 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.
// 3) nav-construct.php : this builds the navigation list
?>

<!-- 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 include('nav-construct.php'); ?>
		<!-- 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)

$navbarTabs = array(
        "home"          =>      "Home",
        "products"      =>      "Products",
        "ordering"      =>      "Ordering Info",
        "press"         =>      "Press Room",
        "events"        =>      "Events",
        "publications"  =>      "Publications",
        "contract"      =>      "Business Development"
);

$navbarTopURLs = array(
        "home"          =>      '#',
        "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;
}

// print title tag on HTML page
echo '<title>SITE NAME : '.$tabName.'</title>';

?>


<!-- nav-construct.php
---------------------------------------- -->
<?php

/* declare global variables 
---------------------------------------- */
global $tab;
global $tabName;
global $page;

global $navbarTabs;
global $navbarURLs;
global $navbarList;

// create navigation 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>";    
// write the list:
echo $navbarList;

?>

Initial URL

                                

Initial Description
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.)

Initial Title
UL Navigation Construction (create re-usable navigations)

Initial Tags
css, php, html

Initial Language
PHP