www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25

Thread: Highlight Current Page Using PHP

  1. #1
    Join Date
    Nov 2011
    Posts
    84

    Highlight Current Page Using PHP

    I found some code that highlights a nav link for each page that you are on this developer site http://www.elanman.co.uk/2009/01/hig...-php-includes/

    I am trying to manipulate a few things so that my "Work" nav stays highlighted for multiple pages not just work.php. Here is the code I have so far from this tutorial

    Original:
    PHP Code:
    <?php
    if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';
    ?>
    HTML Code:
    <ul id="navigation">
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="index.php">Home Page</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="about.php">About Me</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="contact.php">Contact Me</a></li>
        <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'work.php')) echo 'class="current"';?> href="work.php">Work</a></li>
    </ul>
    This is the code found in the same tutorial that explains how to supposedly do what I am trying to do.

    PHP Code:
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php');

    <?php
    foreach ($aboutPages as $page) {
     if (
    strpos($_SERVER['PHP_SELF'], $page)) echo 'class="current"';
    }
    ?>
    In the HTML code above, I am not sure what to insert in the php code in the <a> tag to identify the page. I tried $page but it didn't work. Any thoughts or help?

  2. #2
    Join Date
    Jan 2004
    Posts
    485
    I am not proof positive about what you are trying to do. so this is what I think you are trying to do.


    PHP Code:
    <ul id="navigation">
    <?php 

    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
      echo <
    li>;
      echo 
    '<a href=\''.$page.'\' ';
      (
    strpos($_SERVER['PHP_SELF'], $page))? echo 'class="current"':''
      echo 
    ' > '.$page.'</a>';
      echo </
    li>;

    ?>
    </ul>

    I didn't test but you can try the code above.

    Natdrip :P
    "water go down the hole" - plucky duck

  3. #3
    Join Date
    Nov 2011
    Posts
    84
    So you are saying put it all in the unordered list tag? When I tried that I get errors on "echo <li>;" "(strpos($_SERVER..." and the second "echo </li>;"

    All I'm really trying to do is have my Work link in my main nav stay highlighted for multiple pages, not just work.php (such as work_print.php, work_web.php, etc). Hopefully that explains it better.

  4. #4
    Join Date
    Jan 2004
    Posts
    485
    is the nav an include?
    or is it hard coded on very page?

    if it is hard coded then just add the class='current'

    and build out the css to reflect that.

    if it is included then you'll need to strip the url



    PHP Code:

        
    function ShowFileName($filepath
        { 
            
    preg_match('/[^?]*/'$filepath$matches); 
            
    $string $matches[0]; 
            
    #split the string by the literal dot in the filename 
            
    $pattern preg_split('/\./'$string, -1PREG_SPLIT_OFFSET_CAPTURE); 
            
    #get the last dot position 
            
    $lastdot $pattern[count($pattern)-1][1]; 
            
    #now extract the filename using the basename function 
            
    $filename basename(substr($string0$lastdot-1)); 
            
    #return the filename part 
            
    return $filename
        } 
    then call it
    PHP Code:
            $str $_SERVER['SCRIPT_NAME'];
            
    ShowFileName($str); 

    This should give you the file name

    Then run it through the build array with a conditional that determines which page you should high light.

    kind of what was in the first reply you may have to tweek it.
    Natdrip :P
    "water go down the hole" - plucky duck

  5. #5
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    3,356
    (strpos($_SERVER['PHP_SELF'], $page))? echo 'class="current"':'';
    probably meant
    PHP Code:
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':; 
    or
    PHP Code:
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':echo ''

  6. #6
    Join Date
    Nov 2011
    Posts
    84
    Yes that nav is in an include header.php file. Unfortunately I am not good at writing PHP and I am still not sure what you mean. Any example / tutorial you can point me to?

  7. #7
    Join Date
    Nov 2011
    Posts
    84
    Because unfortunately I need it spelled out for me otherwise I will not figure it out!

  8. #8
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    3,356
    At first glance the code NatDrip initially posted should work, just replace that line that had bad syntax like I suggested.

  9. #9
    Join Date
    Nov 2011
    Posts
    84
    So this? Inside of the <ul> tag? And I added your correction as well.

    PHP Code:
    <ul id="navigation">
    <?php 

    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
      echo <
    li>;
      echo 
    '<a href=\''.$page.'\' ';
    (
    strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;   echo ' > '.$page.'</a>';
      echo </
    li>;

    ?>
    </ul>
    Do I need anything else?

  10. #10
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    3,356
    Did it work? I don't have an environment to test your code in...nor do I have all your code.

  11. #11
    Join Date
    Nov 2011
    Posts
    84
    I get a syntax error on lines 7, 9 and 10

  12. #12
    Join Date
    Nov 2011
    Posts
    84
    This is my original code that I was using. It works for each individual page, but not multiple pages with the same nav element.

    HTML Code:
    <body onload="displayCD()">
    
    <?php
    
    if (strpos($_SERVER['PHP_SELF'], 'about.php'));
    if (strpos($_SERVER['PHP_SELF'], 'index.php'));
    if (strpos($_SERVER['PHP_SELF'], 'work_all.php'));
    
            <div id="main_nav">
            	<ul>
                	<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="active"';?> href="index.php">HOME</a></li>
                    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'work_all.php')) echo 'class="active"';?> href="work_all.php">WORK</a></li>
                    <li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="active"';?> href="about.php">ABOUT</a></li>
                </ul>
            </div>
    Now this is what was recommended but again, there are syntax errors on "echo <li>;", (strpos($_SERVER.....", and "echo </li>;".

    HTML Code:
    <ul id="navigation"> 
    <?php 
    
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php'); 
    
    foreach ($aboutPages as $page) { 
    **echo <li>; 
    **echo '<a href=\''.$page.'\' '; 
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;***echo ' > '.$page.'</a>'; 
    **echo </li>; 
    } 
    ?> 
    </ul>
    Thoughts?

  13. #13
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    3,356
    Quote Originally Posted by andy.richin View Post

    Now this is what was recommended but again, there are syntax errors on "echo <li>;", (strpos($_SERVER.....", and "echo </li>;".

    HTML Code:
    <ul id="navigation"> 
    <?php 
    
    $aboutPages = array('page1.php', 'page2.php', 'page3.php', 'page4.php'); 
    
    foreach ($aboutPages as $page) { 
    **echo <li>; 
    **echo '<a href=\''.$page.'\' '; 
    (strpos($_SERVER['PHP_SELF'], $page))? echo ' class="current"':;***echo ' > '.$page.'</a>'; 
    **echo </li>; 
    } 
    ?> 
    </ul>
    Thoughts?
    Looks like you are missing quotes on your echo. Do you see how the syntax highlighting looks strange? Also I'm assuming your *** are marking the lines with errors and are not actually included.

    PHP Code:
    echo "<li>"

  14. #14
    Join Date
    Nov 2011
    Posts
    84
    That didn't do anything. I attached a screen shot of my code and where the errors are.

  15. #15
    Join Date
    Jan 2009
    Location
    Ohio
    Posts
    3,356
    PHP Code:
    <ul>
    <?php
    $aboutPages 
    = array('page1.php''page2.php''page3.php''page4.php'); 

    foreach (
    $aboutPages as $page) { 
       echo 
    "<li>"
       echo 
    "<a href=\''$page\""
       echo (
    strpos($_SERVER['PHP_SELF'], $page) > 0) ? " class=\"current\""'';
       echo 
    ">$page</a>"
       echo 
    "</li>"

    ?>
    </ul>
    Try that one.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14355 seconds
  • Memory Usage 3,044KB
  • Queries Executed 15 (?)
More Information
Template Usage (38):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_html
  • (10)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (74):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates