stickynote427 Posted April 14, 2010 Share Posted April 14, 2010 I recently wrote some code to create a navigation for part of my website. How it works is that two arrays are given; one contains the text for the links, and the other contains the URLs for the links. It then modifies the URLs a little and creates the links automatically, with dividers. It also disables the link for the current page (so, if there was a link to example.com/products.php, and products.php was actually the page you were on, it would just echo "Products" instead of an A tag). Here is the code: <?php // your domain name $domain = "http://www.example.com"; // the URLs to the pages you want in the navigation $href = array("index.php","about.php","products.php","contact.php"); // the text that will appear inside the A tags $link = array("Home","About Us","Products","Contact Us"); // text used to divide links $div = "|"; for ($i=0;$i<count($href);$i++) { $subcnt = strrpos($_SERVER['PHP_SELF'],"/"); // add the domain to the URLs $href[$i]=$domain.substr($_SERVER['PHP_SELF'],0,$subcnt+1).$href[$i]; // echo the navigation if ($domain.$_SERVER['PHP_SELF'] == $href[$i]) { // if this is the last link, do not add the divider if ($i == count($href)-1) { echo $link[$i]; } // if this is NOT the last link, add the divider else { echo $link[$i]." ".$div." "; } } else { // if this is the last link, do not add the divider if ($i == count($href)-1) { echo "<a href=\"".$href[$i]."\">".$link[$i]."</a>"; } // if this is NOT the last link, add the divider else { echo "<a href=\"".$href[$i]."\">".$link[$i]."</a> ".$div." "; } } } ?> This code works fine on my site, but I have also placed it on my site for others to use. I'm just not sure if it will work if I try to use deeper directories (right now all of the URLs in the $link array link to pages in the same directory as the page running the script) or if others try to modify it for their navigations. Any flaws that might be worth pointing out? Thanks. stickynote427 Link to comment https://forums.phpfreaks.com/topic/198500-navigation-maker-code-i-wrote-does-it-work-well-is-it-efficient/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.