Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Location
    Hillsboro, Oregon
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Applying a class to a div IF on a certian html page, using PHP includes

    I am updating a website, and I am putting in PHP includes for the header and the footer, but I have ran into a little problem.

    On the old version, when you clicked on a link in the NAV, that link would remain highlighted as you viewed that page. They achieved this by manually adding a class in the NAV on that page, and did this for every single page.

    Here is the code showing this: (the class="act" is what they have been manually been adding to the page to get the page to remain highlighted while the user is on that page).

    Code:
    <ul id="nav-ul" class="box">
        <li><a href="index.php">Home</a></li>
        <li><a href="products.php">Products</a>
            <ul>
    	      <li><a href="application.php">Application Services</a></li>
    	       <li><a href="operation-hosting.php">Operations and Hosting Services</a></li>
    		<li><a href="report.php">Reporting Software</a></li>
    		<li><a href="integration.php">Integration Services</a></li>
                    <li><a href="program-management.php">Program Management Office</a></li>
                    <li><a href="edi.php">EDI Services</a></li>
                    <li><a href="training.php" class="no-line">Training</a></li>
              </ul>
        </li>
        <li class="Act"><a href="about-us.php">About Us</a>
               <ul class="aboutUs-list">
                     <li><a href="about-us.php">About Us</a></li>
                     <li><a href="leadership-team.php" class="no-line">Leadership Team</a></li>
              </ul>
        </li>
        <li><a href="news-events.php">News &amp; Events</a></li>
        <li><a href="contact-us.php">Contact Us</a></li>
    </ul>
    With the PHP include, I need to replicate this feature but have it automated. So when the browser loads the page, it says "oh, we are visiting the About Us page, we should apply this class to this list item in the NAV so it remains highlighted for this page."

    Any way to achieve this using PHP or jQuery? I just don't know how to handle it with the PHP inlcude.

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    332
    Thanks
    2
    Thanked 47 Times in 46 Posts
    I would use a combination of PHP and Jquery.

    It looks kinda hacky because I'm mixing PHP and JQuery but it should solve your problem.

    PHP Code:
    <?php

    //This is a php code that is echoed as Js/JQuery code.

    echo $("li:contains('"._FILE_."')").css("color""blue");

    ?>

  • #3
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,138
    Thanks
    7
    Thanked 257 Times in 256 Posts
    Personally I don't like making menus dependent on JS. So I'd do a php version. One simple way is to set a variable to the page name, before the include. Like:
    PHP Code:
    $page "about";
    $include "nav.inc"
    Then in your nav.inc file:
    PHP Code:
    ...
        <li <?php if $page == "about" echo "class='Act'" ?> ><a href="about-us.php">About Us</a>
    ...
    Dave

  • #4
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    or build the whole lot in php on the fly

    PHP Code:
    <?php
        
    $navmenu 
    = array(
        
    'Home' => 'index.php',
        
    'Products' => array(
            
    '0' => 'products.php',
            
    'Application_Services' => 'application.php'
            
    //etc
            
    ),
        
    'About Us' => 'about-us.php'
        
    //etc
        
    );

    $displayedpage $_SERVER['PHP_SELF'];

    foreach (
    $navmenu as $title => $link){
        if (
    is_array($link)){
            if(
    stristr($displayedpage"$link") != FALSE) {$actclass =  'class="Act"';}else{$actclass '';}
                echo 
    '<li'.$actclass.'><a href="'.$navmenu[$title][0].'">'.$title.'</a>';
                echo 
    '<ul>';
                foreach (
    $navmenu[$title] as $title1 => $link1){
                    if (
    $title1 != '0'){
                        if(
    stristr($displayedpage"$link1") != FALSE) {$actclass =  'class="Act"';}else{$actclass '';}
                        echo  
    '<li><a href="'.$link1.'">'.$title1.'</a></li>';
                    }
                }
                echo 
    '</ul>';
        }else{
              echo  
    '<li><a href="'.$link.'">'.$title.'</a></li>';
        }

     
    ?>
    Last edited by Arcticwarrio; 03-21-2013 at 08:07 PM. Reason: udpated, sorry should have tested it first, works now
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #5
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    had a play and refined it a bit

    PHP Code:

     <?php
        
    $navmenu 
    = array(
        
    'Home' => 'index.php',
        
    'Products' => array(
            
    '0' => 'products.php',
            
    'Application_Services' => 'application.php',
            
    'Operations_and_Hosting_Services' => 'operation-hosting.php',
            
    'Reporting_Software' => 'report.php',
            
    'Integration_Services' => 'integration.php',
            
    'Program_Management_Office' => 'program-management.php',
            
    'EDI_Services' => 'edi.php',
            
    'Training' => 'training.php" class="no-line'
            
    ),
        
    'About Us' => 'about-us.php',
        
    'News &amp; Events' => 'news-events.php',
        
    'Contact Us' => 'contact-us.php'
        
    );
            
    $displayedpage $_SERVER['PHP_SELF'];
    echo 
    '<ul id="nav-ul" class="box">
        '
    ;
    foreach (
    $navmenu as $title => $link){
        if (
    is_array($link)){
            if(
    stristr($displayedpage"$link") != FALSE) {$actclass =  ' class="Act"';}else{$actclass '';}
            
    $title str_replace("_"" "$title);
                echo 
    '<li'.$actclass.'><a href="'.$navmenu[$title][0].'">'.$title.'</a>
                    '
    ;
                echo 
    '<ul>
                    '
    ;
                foreach (
    $navmenu[$title] as $title1 => $link1){
                    if (
    $title1 != '0'){
                        if(
    stristr($displayedpage"$link1") != FALSE) {$actclass =  ' class="Act"';}else{$actclass '';}
                        
    $title1 str_replace("_"" "$title1);
                        echo  
    '<li'.$actclass.'><a href="'.$link1.'">'.$title1.'</a></li>
                            '
    ;
                    }
                }
                echo 
    '</ul>
                    '
    ;
        }else{
            if(
    stristr($displayedpage"$link") != FALSE) {$actclass =  ' class="Act"';}else{$actclass '';}
            
    $title str_replace("_"" "$title);
              echo  
    '<li'.$actclass.'><a href="'.$link.'">'.$title.'</a></li>
                  '
    ;
        }

    echo 
    '</ul>';
    ?>
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #6
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    I think some of the solutions are a bit over complicated...

    Code:
    <?php
    $page = home;
    
    include('header.php);
    And in the header include (assuming navigation is in header, if not replace this with any include).

    Code:
    <li <?php if($page == "home") echo 'class ="highlighted"'?>>Home</li>

  • #7
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    I think some of the solutions are a bit over complicated...

    Code:
    <?php
    $page = home;
    
    include('header.php);
    And in the header include (assuming navigation is in header, if not replace this with any include).

    Code:
    <li <?php if($page == "home") echo 'class ="highlighted"'?>>Home</li>

  • #8
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    that requires editing every page you link
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month

  • #9
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by Arcticwarrio View Post
    that requires editing every page you link
    As does every other method.

  • #10
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    709
    Thanks
    20
    Thanked 84 Times in 84 Posts
    mine doesn't,

    this code goes in the header
    There are 10 types of people on CodingForums,
    Those who understand Binary and those who dont.
    Get Cloud Hosting now from only£59 / month


  •  

    Tags for this Thread

    Posting Permissions

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