SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Include Script Whilst Activating Style, Help!?

    Hello i wonder if anyone could help me with this little but essentially very useful problem?

    At the moment i currently use a simple include php script.

    Code:
    <?php require_once('includes/Nav.php'); ?>
    For the usual reason of when the site gets big i only need to change 1 file.

    However, i now need to include a navigation which also has a a:active style on it, i.e when on that page it tells you with the style.

    Is there anyway i can still include this navigation, i cannot think of how to do it because the file will be the same on every page so how will it know its active?

    I have seen big sites where these are used but surely they dont change the navigation of every page, surely they must just change 1 file?

    Can anyone help, or can it not be done?

  2. #2
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can check what page they're on by doing this. If they are on the about.php page, load this file.

    Code PHP:
    if(stristr($_SERVER['HTTP_HOST'],'about.php')){
        require_once('includes/nav.php');
    }
    elseif(stristr($_SERVER['HTTP_HOST'],'contact.php')){
       require_once('includes/nav2.php');
    }

    You can use the same techniques in the stylesheet file. But you will have to rename style.css to style.php and give it a header content-type like this

    Code PHP:
    header("Content-type: text/css");

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, i would still need lots of navs for it to find though, rather than having just one stylesheet to change.

    I found this one but cannot get it to work for some reason...

    Code PHP:
    <?php $currentPage = $_SERVER['REQUEST_URI']; ?>
      <div id="pageNumbers">
        <p><a <?php if ($currentPage == "/home.php") { print 'class="selected"'; } ?> href="/home.php">1</a>
         |<a <?php if ($currentPage == "/contact.php") { print 'class="selected"'; } ?> href="/contact.php">2</a> | 
    </p>
    </div>

    And the css..

    Code:
    div#pageNumbers a{
    	color: Black;
    	text-decoration: none;
    }
    div#pageNumbers a:hover, div#pageNumbers a:focus, div#pageNumbers a:active, div#pageNumbers a.selected{
    	color: purple;
    }

    Any ideas...?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My previous post was supposed to be REQUEST_URI instead of HTTP_HOST.. oops.
    The reason your current one isn't working is because the REQUEST_URI contains the full url, not just what you're comparing it too.

    Try

    Code PHP:
    function classChange($page,$class){
       if(stristr($_SERVER['REQUEST_URI'],$page)){
           return 'class="'.$class.'"';
      }
    }

    Then for the HTML...

    <a href="home.php" <? echo classChange('home.php','selected'); ?>>Home</a>

    Quote Originally Posted by Jezthompson12@nt View Post
    Thanks for that, i would still need lots of navs for it to find though, rather than having just one stylesheet to change.

    I found this one but cannot get it to work for some reason...

    Code PHP:
    <?php $currentPage = $_SERVER['REQUEST_URI']; ?>
      <div id="pageNumbers">
        <p><a <?php if ($currentPage == "/home.php") { print 'class="selected"'; } ?> href="/home.php">1</a>
         |<a <?php if ($currentPage == "/contact.php") { print 'class="selected"'; } ?> href="/contact.php">2</a> | 
    </p>
    </div>

    And the css..

    Code:
    div#pageNumbers a{
    	color: Black;
    	text-decoration: none;
    }
    div#pageNumbers a:hover, div#pageNumbers a:focus, div#pageNumbers a:active, div#pageNumbers a.selected{
    	color: purple;
    }

    Any ideas...?

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again, still doesn't seem to work though

    I popped in what you said see below..

    Code PHP:
    <?php
      function classChange($page,$class){
       if(stristr($_SERVER['REQUEST_URI'],$page)){
           return 'class="'.$class.'"';
      }
    }
    ?>
      <div id="pageNumbers">
        <p><a href="/home.php" <? echo classChange('/home.php','selected'); ?>>1</a>
         |<a  href="/contact.php"<? echo classChange('/contact.php','selected'); ?>>2</a> | 
         </p>
      </div>

    Am i doing something wrong?

    Oh i am including the above in its own nav.php include might that effect something or not?

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check your source to make sure its actually displaying the "class" tag. By the way, there should be a space between "/contact.php" and <? tag, like the home.php tag. When you goto the home.php page it should be displaying class="selected" and when you go to contact it should be displaying class="selected". Lets see the HTML source its generating..

  7. #7
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you view the source its not saying selected its just saying the below..

    Code:
      <div id="pageNumbers">
        <p><a href="/home.php" >home</a>
         <a href="/contact.php" >contact</a>
         </p>
      </div>
       <!--pageNumbers-->
    The php include...

    Code:
    <?php
      function classChange($page,$class){
       if(stristr($_SERVER['REQUEST_URI'],$page)){
           return 'class="'.$class.'"';
      }
    }
    ?>
      <div id="pageNumbers">
        <p><a href="/home.php" <? echo classChange('/home.php','selected'); ?>>home</a>
         <a href="/contact.php" <? echo classChange('/home.php','selected'); ?>>contact</a>
         </p>
      </div>
    ...and not working annoyingly

  8. #8
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note that you have to actually be on the home.php page for it to work. When you go to contact.php (it says it up in your browser URL) does it work? I guess you're putting this all in an include. Make sure its all the same include file for all pages.

    UPDATE: I went and tested this and it works fine for me. When you goto the home.php page it should give you a selected class, when you goto contact.php it should then select the contact link.

  9. #9
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again, i must be doing something wrong if you have got it to work fine.

    I started from scratch with one home page. testHome.php one include testInc.php and another page test1.php to link to.

    Both the testHome and test1 have the testInc however, when you click between them nothing happens.

    I popped it online here

    and the code is as follows....

    testInc.php

    Code:
    <?php
      function classChange($page,$class){
       if(stristr($_SERVER['REQUEST_URI'],$page)){
           return 'class="'.$class.'"';
      }
    }
    ?>
      <div id="pageNumbers">
        <p><a href="/testHome.php" <? echo classChange('/testHome','selected'); ?>>test1</a>
         <a href="/test1.php" <? echo classChange('/test1.php','selected'); ?>>test2</a>
         </p>
      </div>
    testHome and test1..

    Code:
    <title>Untitled Document</title>
    <link href="styles/master.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body>
    <?php require_once('includes/testInc.php'); ?>
    
    
    Test 1
    </body>
    </html>
    and just incase the css...

    Code:
    div#pageNumbers a{
    	color: Black;
    	text-decoration: none;
    }
    div#pageNumbers a:hover, #pageNumbers a:focus, #pageNumbers a:active, #pageNumbers a.selected{
    	color: Purple;
    }
    Have i missed something painfully obvious?

    Many thanks

  10. #10
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know, honestly. I just cut your code and put it in my page and it works perfectly fine.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps i have not got something turned on in my php info?


  12. #12
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok try this. Instead of $_SERVER['REQUEST_URI'] try $_SERVER['PHP_SELF'] and make the HTML

    <a href="test1.php" <? echo classChange('test1.php','selected'); ?>> instead of
    <a href="test1.php" <? echo classChange('/test1.php','selected'); ?>>

    In other words, remove the slash. PHP_SELF targets the exact file name that you're on.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes!

    Thanks that works great.


  14. #14
    SitePoint Addict
    Join Date
    Jul 2006
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to be a pain, but on a side note can i use it twice on 1 page with 2 navigations.

    I did a quick try and got the following error:

    Fatal error: Cannot redeclare classchange() (previously declared on page...

    Anyway i can redeclare, or is that a no no?

    Many thanks once again


Bookmarks

Posting Permissions

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