tmallen Posted May 16, 2008 Share Posted May 16, 2008 I was up late last night hacking on a new (to me) way to do selected links. I got pretty far, but I'd like to hear how you guys do it. Is there a better method than the horribly tedious (and often recommended): <a class="<?php if($page_id == "home") echo "current "; ?>" My solution used a GET variable which mod_rewrite transformed into a friendly URL to identify the current page. Then, the navigation was generated from an associative array for which the key was the link ID, and the value for this key was another array with the human link title and another item for the url. Links are generated from this array (which could be fetched from the DB) and the current page ID is checked against each nav item's key. I can post the code later this evening once I'm out of the office. Quote Link to comment Share on other sites More sharing options...
stylusrose Posted May 16, 2008 Share Posted May 16, 2008 well... you could turn it into a function <a class="<?php pageName($index)?>" or change pageName() to link_home() or something like that... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 Well, if you build your navigation dynamically, you can just test the value against page_id...a simple example: <?php $pages = array( 'home' => 'Home', 'about' => 'About Us', 'contact' => 'Contact Us', ); $page_id = $_GET['page_id']; foreach($pages as $id=>$title) print "<a href=\"index.php?page_id={$id}\"".($id==$page_id?" class=\"current\"":"").">{$title}</a>"; ?> Quote Link to comment Share on other sites More sharing options...
thebadbad Posted May 16, 2008 Share Posted May 16, 2008 I developed a method myself some time ago, using CSS selectors. The PHP and CSS for the current page would look like this: <?php $path = $_SERVER['REQUEST_URI']; //for example "/gallery/summer/" ?> <style type="text/css"> #menu a[href="<?php echo $path; ?>"] { background-color: red; } </style> The above CSS applies to any anchor element, where the "href" attribute is set to $path, inside the element with id="menu". This should work in browsers supporting CSS2. Quote Link to comment Share on other sites More sharing options...
dezkit Posted May 16, 2008 Share Posted May 16, 2008 <?php $page = strtolower($_GET['page']); switch ($page) { case 'home': print 'Current'; break; } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 <?php $page = strtolower($_GET['page']); switch ($page) { case 'home': print 'Current'; break; } ?> How does that help in any way? Quote Link to comment Share on other sites More sharing options...
dezkit Posted May 16, 2008 Share Posted May 16, 2008 no idea Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 no idea and i think it's time for me to call it a day Quote Link to comment Share on other sites More sharing options...
tmallen Posted May 16, 2008 Author Share Posted May 16, 2008 Well, if you build your navigation dynamically, you can just test the value against page_id...a simple example: <?php $pages = array( 'home' => 'Home', 'about' => 'About Us', 'contact' => 'Contact Us', ); $page_id = $_GET['page_id']; foreach($pages as $id=>$title) print "<a href=\"index.php?page_id={$id}\"".($id==$page_id?" class=\"current\"":"").">{$title}</a>"; ?> I think I like this one best. It's like what I was hacking on last night, but cleaner. I hadn't thought of using the key as both the URL and ID; I was defining the two separately. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.