aebstract Posted April 3, 2007 Share Posted April 3, 2007 Hello, I have done something just like what I wana do, I just can't get it to work right now. I want it to run an if statement to see if you are on that page or not and if you are then it will set that link's class to active. If not, doesn't do anything. Here is what I had to do in my previous site, and I can't get it to work on the new one, could someone assist me? Previous working code: echo ' <ul id="navlist"> <li'.(($page == '1' || empty($page)) ? ' class="active"' : '').'><a href="/1/">Uniquely Yours</a></li> <li'.(($page == '2') ? ' class="active"' : '').'><a href="/2/">HAM</a></li> <li'.(($page == '3') ? ' class="active"' : '').'><a href="/3/">MacConnell Motors</a></li> <li'.(($page == '4') ? ' class="active"' : '').'><a href="/4/">Wilder Embry</a></li> <li'.(($page == '5') ? ' class="active"' : '').'><a href="/5/">d.terrell</a></li> <li'.(($page == '6') ? ' class="active"' : '').'><a href="/6/">Last November</a></li> </ul> '; Current links, any help appreciated! (just get me started on one link and ill do the rest) echo '<div id=nav> <a href=index.php>HOME</a> <a href=index.php?page=bios>BIOS</a> <a href=index.php?page=platform>PLATFORM</a> <a href=index.php?page=events>EVENTS</a> <a href=index.php?page=team>TEAM</a> </div>'; The css and everything is fine, just needing to get the class=active to work in the links if possible. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/ Share on other sites More sharing options...
joquius Posted April 3, 2007 Share Posted April 3, 2007 first of all it might be easier to do the following: $links = array ("home", "bios", "platform", "events", "team"); foreach ($links as $link) { echo '<a href="index.php?page=$link" '.(($page == $link) ? 'class="active"' : '').'>$link</a>'; } Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-220705 Share on other sites More sharing options...
aebstract Posted April 3, 2007 Author Share Posted April 3, 2007 That is displaying each link as $link Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-220714 Share on other sites More sharing options...
joquius Posted April 3, 2007 Share Posted April 3, 2007 oh, just escape some stuff, should work echo '<a href="index.php?page='.$link.'" '.(($page == $link) ? 'class="active"' : '').'>'.$link.'</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-220723 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 Okay, sorry I couldn't get back with a response yesterday, but I am having a new problem. That link is showing up in the nav, though they still aren't setting the class to "active" when on that specific page. url: http://jamesvalentine.org Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221158 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 It's been too long for me to edit my last post, but I thought I should post my current code: <?php echo '<div id=nav>'; $links = array ("home", "bios", "platform", "events", "team"); foreach ($links as $link) { echo '<a href="index.php?page='.$link.'" '.(($page == $link) ? 'class="active"' : '').'>'.$link.'</a> '; } echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221199 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 anything? Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221250 Share on other sites More sharing options...
Captain_Pugwash Posted April 4, 2007 Share Posted April 4, 2007 Would you normally set 'class="active"' I would tend to use 'class=active' note the missing inverted Commas CP Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221282 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 That didn't change a thing. Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221285 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 When programming correct HTML, you must quote all attribute values. <?php echo 'class="active"'; ?> Is correct. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221286 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 Okay then, what is wrong with: <?php echo '<div id=nav>'; $links = array ("home", "bios", "platform", "events", "team"); foreach ($links as $link) { echo '<a href="index.php?page='.$link.'" '.(($page == $link) ? 'class="active"' : '').'>'.$link.'</a> '; } echo '</div>'; ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221290 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 Nothing as far as I can tell. I tested your code and it seemed to work fine. What problems are you seeing? Ken Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221300 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 It's not setting the active link's class as active. Whichever page you are on, that link should be class=active so the css steps in with that link. edit: here is how it is generating; <div id=nav><a href="index.php?page=home" >home</a> <a href="index.php?page=bios" >bios</a> <a href="index.php?page=platform" >platform</a> <a href="index.php?page=events" >events</a> <a href="index.php?page=team" >team</a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221302 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 How are you setting "$page"? If you're not doing: <?php $page = $_GET['page']; ?> That is probably your problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221305 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 Uhg, I coulda sworn I was pulling it in out of the index file before I included the nav. Thanks for noticing this! Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221314 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 Within minutes I noticed one last problem. By default the home page won't have a page=bla. With the code that I now have, is it possible to have the HOME tab set as active when nothing is set? Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221321 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You could do something like this: <?php $page = (isset($_GET['page']))?$_GET['page']:'home'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221324 Share on other sites More sharing options...
aebstract Posted April 4, 2007 Author Share Posted April 4, 2007 Wow, thats nice. Thanks a lot for your help everyone! Quote Link to comment https://forums.phpfreaks.com/topic/45454-solved-if-statement-in-url-sorta/#findComment-221333 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.