Ghamdan Posted October 10, 2012 Share Posted October 10, 2012 I am working on chapter 12 and I want to generate a dynamic current class for the navigation links using php for the header.html. Can you help me with that? Thank you. Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 10, 2012 Share Posted October 10, 2012 What have you tried so far? Do you have any code to show us? Link to comment Share on other sites More sharing options...
Ghamdan Posted October 10, 2012 Author Share Posted October 10, 2012 <nav> <ul> <?php $pages = array( 'Home Page' => 'index.php', 'Register' => 'register.php' ); $this_page = basename($_SERVER['PHP_SELF']); foreach($pages as $k => $v) { echo '<li'; if($this_page == $k) { echo ' class="current"'; echo ' > <a href="'.$v.'"> <span> ' . $k . ' </span> </a> </li> '; } }// End of foreach loop ?> </ul> </nav> Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 10, 2012 Share Posted October 10, 2012 If you try to print the string in $this_page, you'll see why it's not working. Do you use a dynamic system for pages, like index.php?page=index? Or how is your link structure? 1 Link to comment Share on other sites More sharing options...
Ghamdan Posted October 10, 2012 Author Share Posted October 10, 2012 I am not using index.php?page=index? I am trying to use the method that I have showed you. I use static links but want to use a dynamic method. Thank you Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 10, 2012 Share Posted October 10, 2012 As I told you, echo out $this_page. You'll then see you'll never get a match. You need a way to match that value with something in your pages array. For that to work, you need to be sure you are comparing the right values. You also do a foreach with $k and $v. Lets say index.php is active. That test will check if "front page" is equal to "something in you link structure." You would more likely check $v. Writing from mobile now, so I'm a little limited here. 1 Link to comment Share on other sites More sharing options...
Ghamdan Posted October 10, 2012 Author Share Posted October 10, 2012 Any help? Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 10, 2012 Share Posted October 10, 2012 <?php // Assign pages $pages = array( 'Home Page' => 'index.php', 'Register' => 'register.php' ); // Get the current page $current = basename($_SERVER['PHP_SELF']); ?> <nav> <ul> <?php foreach ( $pages as $desc => $page) { $active = ($page == $current) ? ' class="current"' : null; echo '<li'.$active.'><a href="'.$page.'"><span>'.$desc.'</span></a>' } ?> </ul> </nav> Link to comment Share on other sites More sharing options...
Ghamdan Posted October 10, 2012 Author Share Posted October 10, 2012 Thank you Antonio Conte, Well done. I appreciate your help. Link to comment Share on other sites More sharing options...
Larry Posted October 10, 2012 Share Posted October 10, 2012 Ghamdan, you should read the forum guidelines. I HATE it when people post completely useless messages like "Any help?" Link to comment Share on other sites More sharing options...
Recommended Posts