#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3

    Dynamic NavBar Problem


    Hello!

    I have having an issue regarding a dynamic output of a NavBar. What I am trying to do is, say for example, a user is on this page (iomegacms [dot] projects [dot] tjswebdev.com [/] otherPage [dot] php?id=3) and in the NavBar I want the page tab to be "active." I have some code in the function as follows:
    PHP Code:
        function getNavBar() {
            
    $query mysql_query("SELECT * FROM pages") or die(mysql_error());
            while(
    $post mysql_fetch_assoc($query)){
                echo 
    "<li class=\"\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
            }
        } 
    What should happen is the li class goes to active when they are on the page that is listed in the bar. I hope this makes sense, if not let me know!

    TJ
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Just a simple If/Then. If your $post['id'] is equal to your $_GET['id'] then 'active'
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Originally Posted by Triple_Nothing
    Just a simple If/Then. If your $post['id'] is equal to your $_GET['id'] then 'active'
    Thank you so much... Here's the problem, I am just starting PHP, and I can't believe I got this far with the project.

    Could you possibly help me or show/tell me how to create an IF/THEN statement for this situation?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Well, first we'll note your address, specifically items after the ? sign. You currently have an id= up there. This will be referenced via $_GET['id']. In the end you've really 2 options. You can run 2 different echo statements, 1 for if selected, 1 for if not. Or if you meerly need to fill in the class attribute of your <li>, you can have a variable set to hold a value, or be blank. The setting of a variable to insert will simply be:

    PHP Code:
    $active = (isset($_GET['id']) && $_GET['id'] == $post['id'] ? "active" ""); 
    You would then set the $active variable like so: echo "<li class=\"" . $active . "\">"

    The other option is to print 2 different echos. This is not always the best way since there's usually no point in writing things more than once...

    PHP Code:
    function getNavBar() {
      
    $query mysql_query("SELECT * FROM pages") or die(mysql_error());
        if(isset(
    $_GET['id']) && $_GET['id'] == $post['id']) {
          echo 
    "<li class=\"active\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
        } else {
          echo 
    "<li class=\"\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
        }
      }

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Okay, thank you. But, the GET ID one, will that check the current URL in the browser to see what page they are on? (otherPage.php?id=8)

    That is still not working. (I dont get a NavBar anymore.)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Did you just try the setting of the $active variable? Can I see your new code?

    Two items in PHP will get variables passed on from a previous page or such from a form. The $_GET['var'] is more address-based. Anything after the question mark in the address bar can be fetched via $_GET[].

    The other mothod is $_POST[]. This is more of an invisible version of $_GET[]. Used the same way and all, but a bit more secure.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Originally Posted by Triple_Nothing
    Did you just try the setting of the $active variable? Can I see your new code?

    Two items in PHP will get variables passed on from a previous page or such from a form. The $_GET['var'] is more address-based. Anything after the question mark in the address bar can be fetched via $_GET[].

    The other mothod is $_POST[]. This is more of an invisible version of $_GET[]. Used the same way and all, but a bit more secure.
    Okay. I changed the code to this, and it is now showing the NavBar, just now it wont show the current page is active (like at the top that says they are on that page):
    PHP Code:
    function getNavBar() {
            
    $query mysql_query("SELECT * FROM pages") or die(mysql_error());
            
    $active = (isset($_GET['id']) && $_GET['id'] == $post['id'] ? "active" ""); 
            while(
    $post mysql_fetch_assoc($query)){
                echo 
    "<li class=\"" $active "\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
            } 
          } 
    Am I suppose to have code on the "otherPage.php" to get the navbar to work besides:
    PHP Code:
    <?php getNavBar(); ?>
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Okay. Right-click your output page and scroll to your navbar's output. Are any errors posted? If not, are all <li> tags cleanly printed <li class="">?

    Just for testing sakes, what I'd do in this situation is place an echo loop for the $_GET['id'] and $post['id'] just to make sure they are holding the variable as desired. And when on a page, does the page id in the address bar match the $post['id']?

    The way you call your navbar() function is fine. It requires nothing to be passed to it, and if it was an issue, it would prolly just post an undefined fuction, or similar.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Originally Posted by Triple_Nothing
    Okay. Right-click your output page and scroll to your navbar's output. Are any errors posted? If not, are all <li> tags cleanly printed <li class="">?

    Just for testing sakes, what I'd do in this situation is place an echo loop for the $_GET['id'] and $post['id'] just to make sure they are holding the variable as desired. And when on a page, does the page id in the address bar match the $post['id']?

    The way you call your navbar() function is fine. It requires nothing to be passed to it, and if it was an issue, it would prolly just post an undefined fuction, or similar.
    Okay... Sorry about delay.

    The tags are be written as <li class="">With the a link stuff in here...</li>

    I did try your other echo idea from a earlier post, and it to didnt work...

    I'm really confused...

    Here is all of the code of the two pages...
    Functions:
    http://pastebin [dot] com/3NTNqCrk

    Other page:
    http://pastebin [dot] com/FGU5C0tj

    Could you help any further?
    Thanks!
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    The line defining active will belong in just your function statement, JUST below your while() line.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Originally Posted by Triple_Nothing
    The line defining active will belong in just your function statement, JUST below your while() line.
    Uhm. Uhm. Uhm. Uhm... IT WORKS!! Now I have a question.


    How come it has to be inside the while like this:
    PHP Code:
        function getNavBar() {
            
    $query mysql_query("SELECT * FROM pages") or die(mysql_error());
            while(
    $post mysql_fetch_assoc($query)){
                
    $active = (isset($_GET['id']) && $_GET['id'] == $post['ID'] ? "active" ""); 
                echo 
    "<li class=\"" $active "\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
            } 
         } 
    And not outside of it like this:
    PHP Code:
        function getNavBar() {
            
    $query mysql_query("SELECT * FROM pages") or die(mysql_error());
            
    $active = (isset($_GET['id']) && $_GET['id'] == $post['ID'] ? "active" "");
            while(
    $post mysql_fetch_assoc($query)){
                echo 
    "<li class=\"" $active "\">" "<a href=\"otherPage.php?id=" $post['ID'] . "\">" $post['Title'] . "</a>" "</li>";
            } 
         } 
    Thanks!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    722
    Rep Power
    7
    Because each loop of the while() is fetching a new value for $post. Also, $post doesn't exist outside the while loop.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    85
    Rep Power
    3
    Originally Posted by Triple_Nothing
    Because each loop of the while() is fetching a new value for $post. Also, $post doesn't exist outside the while loop.
    Oh, okay. Well I want to thank you for your time, I learned a lot!

IMN logo majestic logo threadwatch logo seochat tools logo